All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.frtu.kotlin.utils.io.ResourceHelper.kt Maven / Gradle / Ivy

The newest version!
package com.github.frtu.kotlin.utils.io

import org.slf4j.LoggerFactory
import org.springframework.core.io.DefaultResourceLoader
import java.io.File

/**
 * Allow to easily read data from resources
 *
 * @author Frédéric TU
 * @since 1.1.4
 */
open class ResourceHelper {
    internal val logger = LoggerFactory.getLogger(this::class.java)
    internal val resourceLoader = DefaultResourceLoader()

    fun readFromFile(location: String): String? {
        val absolutePath = resourceLoader.getResource(location).file.absolutePath
        val file = File(absolutePath)
        return if (file.exists() && file.canRead()) {
            logger.debug("Reading file from location:$location & absolutePath:$absolutePath")
            val text = file.readText(Charsets.UTF_8)
            logger.debug("Deserialize text:{}", text)
            text
        } else {
            logger.warn("File doesn't exist or not readable at location:$location & absolutePath:$absolutePath")
            null
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy