[Review] Reading and writing files with java (JDK6)

Purpose

Review before trying java.nio.file.Files (because I haven't done it recently ...).

Source

FileInputOutput.java


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileInputOutput {

	/**
	 *File read / write sample.<br>
	 *Read a file and write it to another file.<br>
	 *The input file is SJIS.<br>
	 *Output file is UTF-8(With BOM)
	 *
	 * @param args
	 */
	public static void main(String args[]) throws Exception {

		BufferedReader br = null;
		BufferedWriter bw = null;

		try {
			//File reading
			//
			File inFile = new File("D:/", "input.csv");
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					inFile), "SJIS"));

			//File output stream
			File outFile = new File("D:/", "output.csv");
			FileOutputStream os = new FileOutputStream(outFile);

			//Grant BOM.
			os.write(0xef);
			os.write(0xbb);
			os.write(0xbf);

			//Generate BufferedWriter
			bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

			String str;

			//Write to file
			while ((str = br.readLine()) != null) {
				bw.write(str + "\n");
			}

			//File deletion(You can delete the read file by uncommenting it.)
			// inFile.delete();
			br.close();
			bw.close();
		} finally {
			if (br != null) {
				br.close();
			}
			if (bw != null) {
				bw.close();
			}
		}
	}
}

Input file

input.JPG

Output file

output.JPG

The contents of the input file could be output in UTF-8 (with BOM) properly.

Recommended Posts

[Review] Reading and writing files with java (JDK6)
[Java] Reading and writing files with OpenCSV
Reading and writing gzip files in Java
[Java] Development with multiple files using package and import
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Use JDBC with Java and Scala.
Writing code with classes and instances
Output PDF and TIFF with Java 8
Drag and drop files with JavaFX
Encrypt with Java and decrypt with C #
With ruby ● × Game and Othello (basic review)
Monitor Java applications with jolokia and hawtio
Java review
Let's try WebSocket with Java and javascript!
[Java] Handle Excel files with Apache POI
[Java] How to output and write files!
Delete folders and files with File Manager
[Kotlin] Delete files with duplicate contents [Java]
Download JDK with Gradle and make JRE
Review notes for Java 1.7 and later file copies
[Java] What should I use for writing files?
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
JSON with Java and Jackson Part 2 XSS measures
Differences in writing Java, C # and Javascript classes
[Beginner] Upload images and files with Spring [Self-satisfaction]
Summarize the differences between C # and Java writing
Prepare a scraping environment with Docker and Java
KMS) Envelope encryption with openssl and java decryption
Encrypt / decrypt with AES256 in PHP and Java
Java installation jdk
[Java] Get metadata from files with Apathce Tika, and get image / video width and height from metadata [Kotlin]
Java and JavaScript
XXE and Java
Java IO review
Use fast Mapping library MapStruct with Lombok and Java 11
Summary of ToString behavior with Java and Groovy annotations
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Solving with Ruby, Perl and Java AtCoder ABC 128 C
[Java] Refer to and set private variables with reflection
Connect with port forwarding with SSH and send and receive files
[Java] About Objects.equals () and Review of String comparisons (== and equals)
I want to transition screens with kotlin and java!
Face recognition app made with Amazon Rekognition and Java
Serverless Java EE starting with Quarkus and Cloud Run