I learned a lot about FileInputStream, BufferedReaderStream, InputStreamReader, etc., but I didn't know which one to use, so I looked it up. It is easy to write using NIO and NIO2.
Oracle Tutorial: File I / O [English] https://docs.oracle.com/javase/tutorial/essential/io/fileio.html [Japanese] https://docs.oracle.com/cd/E26537_01/tutorial/essential/io/fileio.html
NIO (New I/O) API
The factory class Paths for Path and the factory class Files for streams are important. NIO was added in JDK1.4, and NIO2 was added in JDK1.7.
try (Stream<String> ss = Files.lines(Paths.get(filename))) {
ss.forEach(System.out::println);
} catch (IOException e) {
}
try (BufferedReader br = Files.newBufferedReader(Paths.get(filename))) {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
}
try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(filename));
PrintWriter pw = new PrintWriter(bw, true)) {
pw.println(str);
} catch (IOException e) {
}
Recommended Posts