public class FileReEncoding {
String file1 = "E:\\java-fx";
String code1 = "GBK";
String file2 = "E:\\java-fx-2";
String code2 = "UTF-8";
public static void main(String[] args) throws IOException {
FileReEncoding code = new FileReEncoding();
code.processFile(new File(code.file1));
}
public void processFile(File fileIn) throws IOException {
if (fileIn != null) {
if (fileIn.isDirectory()) {
String newDir = fileIn.getCanonicalPath().replace(file1, file2);
File dir = new File(newDir);
if (!dir.exists()) {
dir.mkdir();
}
for (File childFile : fileIn.listFiles()) {
processFile(childFile);
}
} else if (fileIn.getName().endsWith(".java")) {
String filePath = fileIn.getCanonicalPath();
BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(fileIn.toPath()), code1));
String newFile = filePath.replace(file1, file2);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(newFile)), code2));
String line;
while ((line = br.readLine()) != null) {
bw.write(line + "\r\n");
}
br.close();
bw.close();
} else {
FileInputStream fis = new FileInputStream(fileIn);
FileOutputStream fos = new FileOutputStream(fileIn.getCanonicalPath().replace(file1, file2));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
}
}
标签:编码,file1,UTF,String,file2,GBK,fileIn,File,new
From: https://www.cnblogs.com/yantuyouyuxian/p/17132015.html