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

dev.reactant.reactant.extra.file.ReactantTextFileWriterService.kt Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
package dev.reactant.reactant.extra.file

import dev.reactant.reactant.core.component.Component
import dev.reactant.reactant.service.spec.file.text.TextFileWriterService
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers
import java.io.File
import java.io.FileWriter

@Component
class ReactantTextFileWriterService : TextFileWriterService {
    override fun append(file: File, string: String): Completable {
        return write(file, string, true)
    }

    override fun write(file: File, string: String): Completable {
        return write(file, string, false)
    }

    protected fun write(file: File, string: String, append: Boolean): Completable {
        return Completable.fromAction {
            if (file.exists() && !file.isFile) throw IllegalArgumentException(file.name + " is not a file")
            file.parentFile.mkdirs()
            file.createNewFile()

            val writer = FileWriter(file, append)
            writer.write(string)
            writer.flush()

            writer.close()
        }.subscribeOn(Schedulers.io())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy