[Java] How to output and write files!


In file operations, an error called IOEception may occur when accessing a file, so it is good to handle exceptions.

A glossary is attached at the bottom.

As part of my Java study, I started posting because I wanted to output my knowledge. If you have any mistakes, please point them out!

Output the contents of the file

The classes to import are FIle, FileReader, BufferedReader. The flow is ** File selection → Registration → Buffering → Read → Output **.

First, create a File object to select the file to use this time. Enter the file name you want to specify in the argument.


File file = new File("file name");

Next, create a FileReader object to register the selected file. (Here, registration is like a declaration to read a file.) Put a File object in the argument.


FileReader fr = new FileReader(file);

Furthermore, buffering is performed by creating a BufferedReader object so that processing can be performed efficiently. Put the FileReader object in the argument.


BufferedReader br = new BufferedReader(fr);

Read the file contents using the ReadLine method of BufferedReader. The readLine method reads the text line by line and returns null when it reaches the end.

In order to output the read sentence, substitute the statement obtained by the readLine method into the character string variable in the while statement and output it.


String str = "" ;
String data ;

while((data = br.readLine()) != null) {


Write to file

The classes to import are FIleWriter, PrintWriter, BufferedWriter. The flow is ** File selection → Registration + Buffering → Write → Save **.

When writing to a file, create a FileWriter object and select the file. Enter the file name you want to select in the first argument. If you want to add characters in addition to writing / overwriting the file, enter true in the second argument.


FileWriter fw = new FileWriter("file name",true);

After that, create a PrintWriter object to register the file to be written. Define BufferedWriter object with argument. Put the FileWriter object in the argument of the BufferedWriter object.


PrintWriter pw = new PrintWriter(new BufferedWriter(fw));

You can write a string using PrintWriter's println method. Enter the character string you want to write in the argument.

Finally, you can save the written string to a file by using PrintWriter's close method.


pw.println("The character string you want to write");


Store up to a certain amount of data in memory and execute a processing instruction when it is accumulated.

Normally, Reader and Writer classes read and write data in streams, but if the amount of data is large, processing will increase and performance will be poor. Therefore, buffering is used.


Processing data in character units and byte units.

Exception handling

Set in advance what kind of processing should be performed when an error occurs. Use a try-catch statement.

Recommended Posts

[Java] How to output and write files!
[Java] How to get and output standard input
How to write java comments
[Java] Types of comments and how to write them
Studying Java # 6 (How to write blocks)
How to disassemble Java class files
How to decompile java class files
How to write Java variable declaration
How to write and notes when migrating from VB to JAVA
[Introduction to Java] How to write a Java program
How to output Java string to console screen
How to write and explain Dockerfile, docker-compose
Basics of Java development ~ How to write programs (variables and types) ~
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
JDBC promises and examples of how to write
How to output Excel and PDF using Excella
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to get and study java SE8 Gold
How to build parquet-tools and merge Parquet files
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
How to access Java Private methods and fields
[Java] How to use Calendar class and Date class
Java beginner escape boot camp Part 1 Java class structure and how to write
How to record JFR (Java Flight Recorder) and output a dump file
[Java] How to use Map
[Java] How to use Map
Basics of Java development ~ How to write a program (flow and conditional branching) ~
How to uninstall Java 8 (Mac)
How to write good code
Java --How to make JTable
How to use java Optional
Bit Tetris (how to write)
java: How to write a generic type list [Note]
How to minimize Java images
[Refactoring] How to write routing
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
Great poor (how to write)
[Java] How to use string.format
[Note] How to write Dockerfile/docker-compose.yml
How to use Java Map
How to set Java constants
How to write Junit 5 organized
How to convert A to a and a to A using AND and OR in Java
How to write Rails validation
How to handle TSV files and CSV files in Ruby
How to write Rails seed
How to use Java variables
[Ruby] How to write blocks
How to convert Java radix
How to write modern Java. Immutable Java, consider Null safety.
How to write Rails routing
[Java] How to implement multithreading
[Java] How to use Optional ①