Home >Java >javaTutorial >How to use Java to convert GBK project to uft8 method sharing
This article mainly introduces the method examples of converting GBK projects to uft8 using Java. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
This article introduces how to use java to convert GBK projects to uft8 and share it with everyone. The details are as follows:
The default encoding under windows is GBK and gb2312. How to convert gbk's java project to utf8? If you modify the project encoding directly, the Chinese characters in the java files inside will actually be garbled. I wrote a program for batch conversion of java projects for fun.
Why transcode?
Some old projects, or friends’ projects, didn’t notice before that they were not UTF8 on Windows, and if you need to read comments or something, you can’t change the encoding attributes one file at a time.
The trial scope of this program
gbk code or gb2312 project can be converted
The idea of coding conversion
Originally I wanted to make a universal program that can automatically detect encoding and convert automatically. However, due to the inaccurate judgment of the encoding type, conversion to GBK was made.
Specify gbk encoding to read the file stream, load it into memory, and convert it to String type content
Convert String content to utf8 String
Write String content to file
Core code:
public class TransferProject{ public static void transferFile(String pathName,intdepth)throwsException{ File dirFile = new File(pathName); if (!isValidFile(dirFile)) return; //获取此目录下的所有文件名与目录名 String[] fileList = dirFile.list(); int currentDepth = depth + 1; for (int i = 0; i < fileList.length; i++) { String string = fileList[i]; File file = new File(dirFile.getPath(), string); String name = file.getName(); //如果是一个目录,搜索深度depth++,输出目录名后,进行递归 if (file.isDirectory()) { //递归 transferFile(file.getCanonicalPath(), currentDepth); } else { if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) { readAndWrite(file); System.out.println(name + " has converted to utf8 "); } } } } private static boolean isValidFile(File dirFile)throwsIOException{ if (dirFile.exists()) { System.out.println("file exist"); return true; } if (dirFile.isDirectory()) { if (dirFile.isFile()) { System.out.println(dirFile.getCanonicalFile()); } return true; } return false; } private static void readAndWrite(File file)throwsException{ String content = FileUtils.readFileByEncode(file.getPath(), "GBK"); FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8")); } public static void main(String[] args)throwsException{ //程序入口,制定src的path String path = "/Users/mac/Downloads/unit06_jdbc/src"; transferFile(path, 1); } }
public class FileUtils{ public static void writeByBufferedReader(String path, String content){ try { File file = new File(path); file.delete(); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file, false); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } public staticStringreadFileByEncode(String path, String chatSet)throwsException{ InputStream input = new FileInputStream(path); InputStreamReader in = new InputStreamReader(input, chatSet); BufferedReader reader = new BufferedReader(in); StringBuffer sb = new StringBuffer(); String line = reader.readLine(); while (line != null) { sb.append(line); sb.append("\r\n"); line = reader.readLine(); } return sb.toString(); } }
The above is the detailed content of How to use Java to convert GBK project to uft8 method sharing. For more information, please follow other related articles on the PHP Chinese website!