Let's write Java file input / output with NIO


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


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.

Text input

1. If you want to get a stream for each row

try (Stream<String> ss = Files.lines(Paths.get(filename))) {
} catch (IOException e) {

2. If you want to read and process line by line

try (BufferedReader br = Files.newBufferedReader(Paths.get(filename))) {
    String line = null;
    while ((line = br.readLine()) != null) {
} catch (IOException e) {

Text output

try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(filename));
    PrintWriter pw = new PrintWriter(bw, true)) {
} catch (IOException e) {


Recommended Posts

Let's write Java file input / output with NIO
Let's scrape with Java! !!
Java (jdk1.8 or later) file input / output sample program
csv file output with opencsv
Let's experiment with Java inlining
Let's operate Excel with Java! !!
[Java] I want to test standard input & standard output with JUnit
Log output to file in Java
Output PDF and TIFF with Java 8
EXCEL file update sample with JAVA
Java file input / output processing that can be used through historical background
Let's try WebSocket with Java and javascript!
[Java] How to output and write files!
Read xlsx file in Java with Selenium
Implement image input / output with Spring MVC
I can't remember the text file input / output in Java, so I summarized it.
Just input and output images with Spring MVC
Read a string in a PDF file with Java
[LeJOS] Let's control the EV3 motor with Java
Let's create a timed process with Java Timer! !!
Let's easily write multiple loops with swift closure
[Java] How to get and output standard input
Try to implement TCP / IP + NIO with JAVA
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] Convert and import file values with OpenCSV
[Java] output, variables
Ruby input / output
Let's write Javadoc
Let's study Java
java file creation
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
I tried Java Lambda input / output type ~ POJO edition ~
How to input / output IBM mainframe files in Java?
Include image in jar file with java static method
Output log to external file with slf4j + logback with Maven
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
[Java basics] Let's make a triangle with a for statement
I tried Java Lambda input / output type ~ Stream version ~
Quick learning Java "Introduction?" Part 2 Let's write the process
[Java] Get the file path in the folder with List
I tried OCR processing a PDF file with Java
[LeJOS] Let's remotely control the EV3 motor with Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
paiza skill check standard input / output summary [Java edition]
Gzip-compress byte array in Java and output to file