Shift-JIS形式を一括でUTF-8形式にして出力するJavaプログラム
使い方
UTF-8形式に変換したいテキストファイルを一つのフォルダに集めます。
"フォルダーのパス"と書かれたところにフォルダの場所を書きます。
パスのところは以下のように書きます。(エスケープ処理のために「\」をひとつ足す必要があると思います)
例)C:\Users\YourName\Desktop\Folder
実行すると「converted」というフォルダがファイルと同じ場所に作られ、中に変換されたファイルが保存されます(Shift-JIS以外は処理しません)。
作った理由
iPhoneからAndroidに乗り換えた際に、使っていたメモアプリ(Textforce)がAndroidに無かったため別のメモアプリ(Simplenote)に乗り換え、それまでのメモを移植したところ文字コードの違いで、PCで作成したメモが全て文字化けしてしまったので。
Androidで使えてShift-JIS形式のテキストを読み込んでくれてパソコン・スマホで簡単に同期できてかつ軽いアプリがあれば自分でこんなことする必要は無かったのに・・・
誰かおすすめあれば教えてください
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("フォルダーのパス"));
}
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 + "はSJISじゃありません");
}
} catch (IOException e) {
System.out.println("IOExceptionエラー");
}
}
}
}
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;
}
}
}
Author And Source
この問題について(Shift-JIS形式を一括でUTF-8形式にして出力するJavaプログラム), 我々は、より多くの情報をここで見つけました https://qiita.com/Mutz/items/a439ee9358d02a2a499c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .