As we all know, the Chinese problem of Java has always been an old and difficult problem. This phenomenon can be found in the Java forum of csdn regarding the question rate of Chinese garbled characters, even the software giant IBM has opened up a special area to solve Chinese problems. As a common man (who has always dreamed of becoming a "fake god" like Jin Kelly, but has never been successful, maybe it is because God does not take care of China.) I am also troubled by the issue of consent. Fortunately, today we have solved it.
With. NET platform to give up completely (just by the pressure of life, after all, I want to live), the original resources to the Java platform is one of my current important tasks, so here, I have to admit. net solves Chinese problems very well, although occasionally garbled.
The problem is that my box (http://www.ibeyond.org/box/) was originally written using Asp.net (only to try new technology) and I gave up using the database for data portability, save all the data as a UTF-8-encoded text file in. to read this file order in. net, you only need to specify the encoding method for opening the file, which can avoid Chinese characters. However, according to my previous thinking, this is not the case in Java. Java reads and converts them first. The final result proves that I am wrong. It seems that the idea of experience has killed people.
File F = new file (New boxconfig (). getbasedir () + "Data //" + request. getparameter ("ID") + ". Inc ");
Bufferedreader reader = new bufferedreader (New filereader (f ));
String line;
While (line = reader. Readline ())! = NULL ){
Strcontent + = line;
}
This is my initial code, F, which is the text file in UTF-8 format that I want to read, and this day has gone through countless attempts, including new string (strcontent. getbytes ("UTF-8") (where n encoding conversions are attempted), system. setproperty ("file. encoding "," UTF-8 ") (which specifies n types of encoding conversion), all failed, in which I believe that you have experienced the program torture of all the readers understand, here skipped. Finally, I got a piece of information that proved to be important in the numerous Google queries, "specifying character encoding when reading files ,. is that true in net? A detour finally solved this problem.
File F = new file ("E: // eclipse // workspace // box // data // 2003-3-7-100.inc ");
Inputstreamreader READ = new inputstreamreader (New fileinputstream (F), "UTF-8 ");
Bufferedreader reader = new bufferedreader (read );
String line;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
By opening a file, you can specify the file encoding method to solve this problem easily.
To sum up, Java must specify the data stream encoding method when reading data streams (at least when reading string streams.