There are many classes for reading and writing files, but I didn't know which class was best to use. In the old days, I used to refer to the code in another project ~~ copy ~~. So I looked it up. In conclusion, the more I looked up, the more confused I was. And use this! !! I didn't find anything like that.
Process the data in bytes. ~~ By the way, there is no class called Stream. ~~ interface https://docs.oracle.com/javase/jp/8/docs/api/java/util/stream/Stream.html
InputStream A class that does ** "read" ** with bytes.
OutputStream A class that does ** "write" ** with bytes.
FileInputStream A class for reading bytes. A child class of InputStream. It's quite old.
FileOutputStream A class for writing bytes. A child class of OutputStream. It's quite old.
InputStreamReader The role of passing the InputStream to the Reader. Specify the character code here.
OutputStreamWriter The role of passing the Output Stream to the Writer. Specify the character code here.
Reader Do ** "read" ** from the stream in character units (char [] in code) instead of bytes.
Writer Do ** "write" ** from the stream in character units (char [] in code) instead of bytes.
A function that stores in memory and reads and writes at once when a certain amount is accumulated. As a result, the performance of reading and writing byte by byte is good.
BufferedReader With Reader buffering function
BufferedWriter With Writer buffering function
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileCreate {
public static void main(String[] args) {
//File path to read and write
Path path = Paths.get("C:\\demo.txt");
//Character string to write
List<String> lines = Stream.of("AIUEO", "Kakikukeko", "SA Shi Su Se So").collect(Collectors.toList());
//File writing 1
//Character code is UTF by default-8
//I'm using OutputStreamWriter
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
for (String str : lines) {
//Note that if you pass null, it will be written as a character string called null.
writer.append(str);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
//File writing 2
//Internally uses BufferedWriter, OutputWriter and FileOutputStream
try {
//When you want to write a new one
Files.write(path, lines, Charset.forName("Shift_JIS"), StandardOpenOption.WRITE);
//When you want to add
Files.write(path, lines, Charset.forName("Shift_JIS"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
//File writing 3
// printWriter
try (
//True if you want to add, false if new
FileOutputStream fileOutputStream = new FileOutputStream(path.toFile(), true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charset.forName("Shift_JIS"));
PrintWriter printWriter = new PrintWriter(outputStreamWriter)) {
for (String s : lines) {
printWriter.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
//File writing 4
// BufferedWriter
try (
//True if you want to add, false if new
FileOutputStream fileOutputStream = new FileOutputStream(path.toFile(), true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charset.forName("Shift_JIS"));
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
for (String s : lines) {
bufferedWriter.append(s);
bufferedWriter.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you want to keep it simple, I think you can write file 1 or 2. However, if you want to customize the writing contents to your liking, I will use file writing 3 or 4.
File writing 3 and 4 are easy for me to understand and declare variables one by one. Of course, it's OK to put them together in one line.
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path.toFile(), true), Charset.forName("Shift_JIS")));
One thing to keep in mind is that you don't forget close ().
I spent about two days researching it, but I could roughly understand it. However, I feel that I do not understand the details. If it's wrong, or is this code useless? I would appreciate it if you could point out any points.
Recommended Posts