This is a method for outputting or reading a file using SJIS when outputting to a file using Kotlin (Java).
import java.io.FileWriter
import java.nio.charset.Charset
FileWriter(fileName, Charset.forName("SJIS")).use {
it.write("...")
}
import java.io.FileOutputStream
import java.io.OutputStreamWriter
import java.nio.charset.Charset
OutputStreamWriter(
FileOutputStream(fileName, true), Charset.forName("SJIS").displayName()
).use {
it.write("...")
}
If there are many scenes to output in SJIS, it is convenient to create the following class.
import java.io.FileOutputStream
import java.io.OutputStreamWriter
import java.nio.charset.Charset
class SjisFileWriter(fileName: String, append: Boolean) : OutputStreamWriter(
FileOutputStream(fileName, true), Charset.forName("SJIS").displayName()
)
import java.io.FileInputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
class SjisFileReader(fileName: String) :
InputStreamReader(FileInputStream(fileName), Charset.forName("SJIS")) {
}
Recommended Posts