I really don't know much about IO operations. . . I also have little knowledge of coding and garbled characters. . . Today I encountered a requirement to convert a file's encoding and return an encoded string, such as the original GBK encoding, converted to UTF-8
Among them, the BytesEncodingDetect class will not be posted. Mainly used to obtain the file encoding format.
I first tried modifying the encoding method directly in the source file and using URLEncoder and URLDecoder for conversion, but it still didn't work. The last word of an odd number in Chinese is garbled.
Baidu looked for a solution, but failed, so they had to adopt my idea: first read the content of the source file, store it in the StringBuffer, then delete the source file, then create a new file, and then store it in another encoding form.
Check the encoding effect: Be careful not to check the effect in eclipse. Eclipse only views it in an encoding form, so you can view HTML files on the browser side. To view the specified encoding, you can right-click --- encoding to determine whether it is successful.
Copy the code code as follows:
package com.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class Transcoding {
private BytesEncodingDetect encode = new BytesEncodingDetect();
public Transcoding() {
}
/**
* Code conversion
* @param toCharset The encoding to be converted
* @param path The file path to be converted
* @return
* @throwsException
*/
public String encoding(String toCharset, String path) throws Exception{
File srcFile = new File(path);
int index = encode.detectEncoding(srcFile);
String charset = BytesEncodingDetect.javaname[index];
// The encoding is the same, no need to transcode
if (charset.equalsIgnoreCase(toCharset)) {
return "The encoding is the same, no conversion is needed";
}
InputStream in = new FileInputStream(path);
BufferedReader br = new BufferedReader(
new InputStreamReader(in, charset));
StringBuffer sb = new StringBuffer();
String s1;
while ((s1=br.readLine())!=null) {
String s = URLEncoder.encode(s1, toCharset);
sb.append(s+"/r/n");//One line + carriage return
}
br.close();
srcFile.delete();//Delete the original file
//Rewrite the file with the new encoding and return the value
File newfile = new File(path);//Rebuild the original file
newfile.createNewFile();
OutputStream out = new FileOutputStream(newfile);
OutputStreamWriter writer = new OutputStreamWriter(out, toCharset);
BufferedWriter bw = new BufferedWriter(writer);
bw.write(URLDecoder.decode(sb.toString(), toCharset));
String result = URLDecoder.decode(sb.toString(), toCharset);
bw.flush();//Flash into the file
bw.close();
return result;
}
}