Write to a file using ShiftJIS-Read a file (Kotlin / JVM)

This is a method for outputting or reading a file using SJIS when outputting to a file using Kotlin (Java).

When writing to a file

Use FileWriter

import java.io.FileWriter
import java.nio.charset.Charset

FileWriter(fileName, Charset.forName("SJIS")).use {
    it.write("...")
}

Use OutputStreamWriter

import java.io.FileOutputStream
import java.io.OutputStreamWriter
import java.nio.charset.Charset

OutputStreamWriter(
    FileOutputStream(fileName, true), Charset.forName("SJIS").displayName()
).use {
    it.write("...")
}

Create a dedicated class

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()
)

When reading a file

Use FileReader

Use InputStreamWriter

Create a dedicated class

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

Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
To write a user-oriented program (1)
Upload a file using Java HttpURLConnection
How to write a ternary operator
How to create a jar file or war file using the jar command
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to sort a List using Comparator
I want to write a nice build.gradle
[Basic] How to write a Dockerfile Self-learning ②
Building a Kotlin development environment using SDKMAN
[Introduction to Java] How to write a Java program
I want to write a unit test!
[Personal memo] Writing a file using BufferedWriter
Reading a file using Java's Scanner class
[Personal memo] Reading a file using BufferedReader
[SpringBoot] How to write a controller test
I want to be able to read a file using refile with administrate [rails6]
How to create an Excel form using a template file with Spring MVC
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
Rails: How to write a rake task nicely
Scraping and writing specific elements to a file
[Rails] How to write when making a subquery
Write a dockerfile to start jupyter-lab on ubuntu
Build a Kotlin app using OpenJDK's Docker container
[Ethereum] How to execute a contract using web3j-Part 2-
I want to simply write a repeating string
A story about trying to operate JAVA File
To manually deploy Struts2 as a war file
How to generate a primary key using @GeneratedValue
Try to make a music player using Basic Player
How to write Java String # getBytes in Kotlin?
I tried to implement a server using Netty
I want to implement it additionally while using kotlin on a site running Java
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Studying java8 (such as reading a file using Stream)
How to jump from Eclipse Java to a SQL file
How to write a unit test for Spring Boot 2
How to unit test with JVM with source using RxAndroid
java: How to write a generic type list [Note]
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Add a subview to your animation layer using lottie-ios
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Write a class in Kotlin and call it in Java
A memorandum when trying to create a GUI using JavaFX
How to delete custom Adapter elements using a custom model
I want to monitor a specific file with WatchService
Convert csv file to fixed length record file using enum
What to do when rails creates a 〇〇 2.rb file
How to write a date comparison search in Rails
[Introduction to Spring Boot] Submit a form using thymeleaf
How to download a file (Servlet, HTML, Apache, Tomcat)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
How to convert A to a and a to A using AND and OR in Java
Write byte array image data to outputstream using ImageIO
How to convert a file to a byte array in Java
Try to build a Java development environment using Docker
How to write a core mod in Minecraft Forge 1.15.2
[Rails] How to install a decorator using gem draper
How to dynamically write iterative test cases using test / unit (Test :: Unit)