文件编码转换(GBK转UTF-8)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件编码转换(GBK转UTF-8)相关的知识,希望对你有一定的参考价值。
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();
以上是关于文件编码转换(GBK转UTF-8)的主要内容,如果未能解决你的问题,请参考以下文章