I can't remember the text file input / output in Java, so I summarized it.

For some reason, every time a project changes, so does the programming language used. Every time, I google how the text file was input and output. The current site is Java, so let's summarize it!

Read

final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
StringBuffer sb = new StringBuffer();
String[] lineSeparator = { "" };
BufferedReader br = new BufferedReader(new FileReader(f));
br.lines().forEach(line -> {
	sb.append(lineSeparator[0]);
	sb.append(line);
	lineSeparator[0] = System.lineSeparator();
});
br.close();
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
StringBuffer sb = new StringBuffer();
String[] lineSeparator = { "" };
try (BufferedReader br = new BufferedReader(new FileReader(f));) {
	br.lines().forEach(line -> {
		sb.append(lineSeparator[0]);
		sb.append(line);
		lineSeparator[0] = System.lineSeparator();
	});
} catch (Exception e) {
	e.printStackTrace();
}

writing

final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
lines.forEach(line -> {
	try {
		bw.write(line);
		bw.newLine();
	} catch (IOException e) {
		e.printStackTrace();
	}
});
bw.close();
final String baseDir = "/Users/yasuhiro/Documents/tmp";
File f = new File(baseDir + "/timestamp.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f));) {
	lines.forEach(line -> {
		try {
			bw.write(line);
			bw.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	});
} catch (IOException e) {
	for (Throwable t : e.getSuppressed()) {
		t.printStackTrace();
	}
}

Recommended Posts

I can't remember the text file input / output in Java, so I summarized it.
I stumbled on the Java version in Android Studio, so I will summarize it
Log output to file in Java
I tried the input / output type of Java Lambda ~ Map edition ~
I passed the Oracle Java Bronze, so I summarized the outline of the exam.
I don't understand the devise_parameter_sanitizer method, so I'll output it here.
The corretto I put in Homebrew disappeared after updating, so I fixed it.
I tried the new era in Java
Memo: [Java] If a file is in the monitored directory, process it.
[day: 5] I summarized the basics of Java
[Java] I personally summarized the basic grammar.
Console input in Java (understanding the mechanism)
I received the data of the journey (diary application) in Java and visualized it # 001
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
Let's write Java file input / output with NIO
[Java] Read the file in src / main / resources
I tried to output multiplication table in Java
[Java] Something is displayed as "-0.0" in the output
[Java] Integer information of characters in a text file acquired by the read () method
I tried Java Lambda input / output type ~ POJO edition ~
Java (jdk1.8 or later) file input / output sample program
How to input / output IBM mainframe files in Java?
I couldn't run it after upgrading the Java version
[Java] Get the file path in the folder with List
I introduced Docker to Rails 6, so I summarized it (beginner)
I summarized the types and basics of Java exceptions
I tried to implement the Euclidean algorithm in Java
Gzip-compress byte array in Java and output to file
I noticed that I am developing Java FW in the mono repo, so I will consider the operation.
I got stuck in launching Unreal Enigine 4 on an AWS Ubuntu instance, so I summarized it
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
parquet-tools gives java.lang.ExceptionInInitializerError, so I made it work with java8
[Java] ArrayList → Should I specify the size in array conversion?
Java SE 13 (JSR388) has been released so I tried it
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
[Java] I want to test standard input & standard output with JUnit
I want to simplify the conditional if-else statement in Java