Java program that outputs Shift-JIS format in UTF-8 format at once

** How to use ** Collect the text files you want to convert to UTF-8 format in one folder. Write the location of the folder where it says "folder path". Write the path as follows. (I think you need to add one "\" for escaping) Example) C: \ Users \ YourName \ Desktop \ Folder

When executed, a folder called "converted" will be created in the same location as the file, and the converted file will be saved inside (except for Shift-JIS).

** Reason for making ** When I switched from iPhone to Android, I didn't have the memo app (Textforce) I was using on Android, so I switched to another memo app (Simplenote), and when I ported the previous memos, the memos created on the PC were different due to the difference in character code. Because all the characters have been garbled. If there was a light app that could be used on Android and read Shift-JIS format text and could be easily synchronized with a computer or smartphone, I wouldn't have to do this myself ... Please let me know if anyone recommends

SJIS2UTF8.java


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class SJIS2UTF {
	public static void main(String[] args) {
		SJIS2UTF stu = new SJIS2UTF();
		stu.readFolder(new File("Folder path"));
	}

	public void readFolder(File dir) {
		File[] files = dir.listFiles();
		String fileName;
		if (files == null) {
			return;
		}
		for (File file : files) {
			if (!file.exists()) {
				continue;
			} else if (file.isDirectory()) {
				readFolder(file);
			} else if (file.isFile()) {
				try {
					byte[] byteOfFile = convertFile(file);
					fileName = file.getName();

					if (isSJIS(byteOfFile)) {
						encodeStoU(file, fileName);
					} else {
						System.out.println(fileName + "Is not SJIS");
					}
				} catch (IOException e) {
					System.out.println("IOException error");
				}
			}
		}
	}

	public void encodeStoU(File file, String fileName) {
		String parentPath = file.getParent();
		String sourcePath = parentPath + "\\" + fileName;
		String destFolder = parentPath + "\\converted";
		String destPath = parentPath + "\\converted\\" + fileName;

		File folder = new File(destFolder);
		if (!folder.exists()) {
			try {
				Files.createDirectory(Paths.get(destFolder));
				Files.createFile(Paths.get(destFolder, fileName));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

		try {
			File sourceFile = new File(sourcePath);
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), "Shift-JIS"));
			File destFile = new File(destPath);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"));

			String line;
			while ((line = br.readLine()) != null) {
				bw.write(line);
				bw.newLine();
			}
			br.close();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public byte[] convertFile(File file) throws IOException {
		return Files.readAllBytes(file.toPath());
	}

	public static boolean isSJIS(byte[] src) {
		try {
			byte[] tmp = new String(src, "Shift_JIS").getBytes("Shift_JIS");
			return Arrays.equals(tmp, src);
		} catch (UnsupportedEncodingException e) {
			return false;
		}
	}
}

Recommended Posts

Java program that outputs Shift-JIS format in UTF-8 format at once
Java that outputs vmg format file in eml format
A program (Java) that outputs the sum of odd and even numbers in an array
Null-safe program in Java (Eclipse)
Sample program that returns the hash value of a file in Java
Try using JSON format API in Java
Output Date in Java in ISO 8601 extended format
Program PDF headers and footers in Java