Next, I will transfer a Java code to identify the file, convert the code, and solve the Chinese garbled code instance program. I hope the program will be helpful to you.
| The Code is as follows: |
Copy code |
Try {
File file = new File (lrcPath );
// Obtain the encoding of the original file
CodepageDetectorProxy detector = CodepageDetectorProxy. getInstance ();
Detector. add (JChardetFacade. getInstance ());
Charset charset = null;
Charset = detector. detectCodepage (file. toURL ());
If (charset! = Null ){
// Use the source file encoding to read the file
InputStreamReader bufRead = new InputStreamReader (new FileInputStream (file), charset. name ());
StringBuilder sb = new StringBuilder ();
Char [] buffer = new char [2048];
Int len = 0;
While (len = bufRead. read (buffer, 0, 2048)> 0 ){
Sb. append (buffer, 0, len );
}
String text = sb. toString ();
BufRead. close ();
// Write files with UTF-8 Encoding
BufferedWriter bufWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), "UTF-8 "));
BufWriter. write (text );
BufWriter. flush ();
BufWriter. close ();
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} |