Reading and writing Java basic files

Read and write files using FileWriter and FileReader

First, specify the file name and create a File object. Based on that, the character streams "** FileWriter " and " FileReader **" are used to read and write in char units.

By the way, if you use the character stream, the character code is automatically converted, so you can input and output without being aware of the character code of the OS where the file is saved.

The following code creates a file called "text.txt", writes it, and then It is a program that reads the contents of the file and outputs it.

Main.java


import java.io.*;

public class Main {
	public static void main(String[] args) {
		try (FileWriter text1 = new FileWriter(new File("text.txt"));
				FileReader text2 = new FileReader(new File("text.txt"))) {
			text1.write("Hello.");
			text1.flush();
			int i = 0;
			while ((i = text2.read()) != -1) {
				System.out.print((char) i);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


You are using the ** flush () method ** after the written as "Hello", but this means the process of instruction to perform writing to force the file at the moment of the call.

The previous ** write () method ** requests the write of data, but it is not always written to the file immediately after calling this method. In this program, we want to read and output the first written character, so we command forced writing so that writing is always done before reading.

Also, in the while statement, the ** read () method ** of the FileReader class is used to get the characters written in the file one by one and return them as an int type. The read () method reads one character each time it is executed, and returns -1 when the end of the file is reached, so that condition is included in the judgment statement.

In order to output the int type information acquired by the read () method as a character, an explicit cast to the char type is required, so a cast expression is written and converted to the char type.

You can read and write files by doing this.

Next time, I would like to describe the BufferedReader, BefferedWriter classes, serialization, etc.

Recommended Posts

Reading and writing Java basic files
[Java] Reading and writing files with OpenCSV
Reading and writing gzip files in Java
[Review] Reading and writing files with java (JDK6)
I tried to chew C # (reading and writing files)
[Java] Basic types and instruction notes
Basic data types and reference types (Java)
Java basic data types and reference types
[Java] Exception types and basic processing
[Java] How to output and write files!
Basic knowledge of Java development Note writing
[Note] Cooperation between Java and DB (basic)
Java basic grammar
Java basic grammar
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Notes for reading and generating xlsx files from Java using Apache POI
Java and JavaScript
XXE and Java
Java basic grammar
Java exercises [Basic]
[Java] What should I use for writing files?
[Java] Personal summary of classes and methods (basic)
Differences in writing Java, C # and Javascript classes
Summarize the differences between C # and Java writing
Introduction to Apache Beam (1) ~ Reading and writing text ~
Getters and setters (Java)
[Java] Thread and Runnable
java basic knowledge memo
[Java] Data type ①-Basic type
Java basic date manipulation
Ruby Learning # 27 Writing Files
Java basic naming conventions
Java learning memo (basic)
Java --Serialization and Deserialization
[Java] Arguments and parameters
About Java basic data types and reference type memory
timedatectl and Java TimeZone
SystemSpec introduction and writing
[Java] Branch and repeat
Ruby Learning # 26 Reading Files
[Java] Variables and types
[Java] Basic method notes
java (classes and instances)
[Java] Development with multiple files using package and import
Java basic data types
[Java] Overload and override
Basic Java OOps concepts
Basic operators and operations
* Android * About saving / reading files to external storage and permissions
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Implement writing and reading to Property List (.plist) in Swift
Java basic learning content 7 (exception)
Java version 8 and later features
Read binary files in Java 1
Basic Authentication with Java 11 HttpClient
Java basic syntax + α trap
Basic data type and reference type
[Java] Stack area and static area