Java read files containing Chinese solutions

zhaozj2021-02-17  54

Java read files containing Chinese solutions

Tormed a day, finally solved at dusk, now live fast.

As we all know, Java's Chinese issue has always been an old difficult problem. This phenomenon can be learned in the Java Forum in the Java Forum in 9CBS, and even software giant IBM has opened up a zone to solve Chinese problems. As a Troupe (have always dream of thinking about Jin Kelly as a "counterfeit God", but has not been successful, maybe because God does not take care of China), I also happen to the agreement, so I solved it today.

As I have given up the .NET platform (just forced by life, I still have to live), transfer the original resources to the Java platform, one of my current important tasks, saying this, I have to admit .NET solves very well on Chinese issues, although occasionally garbled.

The problem is like this, my box (http://www.ibeyond.org/box/) is originally written using ASP.NET (I just try new technologies), when I gave up for data. Using the database, save all the data as UTF-8 encoded text files, read this file order in .NET As long as the coding method of the file is specified, the Chinese problem can be avoided. But according to my previous thinking, Java is not in this, Java is read first after reading, the last result proves that I am wrong, it seems that the experience is harm to death.

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 original code f is the text file I want to read. This day has a numerous attempt, including New String (StrContent.getbytes ("UTF-8")) (attempted N Coded Conversion), System.SetProperty ("File.Encoding", "UTF-8") (which specifies N kinds of encoding conversions), all fail, where you are struggling to have experienced all of the programs tortured I have been understood, I will be slightly. Finally, in the countless queries in Google, a subsequent information "read files when reading files", the word is awake in the middle of the dream, isn't the .NET is not so real? I have 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);

This problem is easily solved by opening a file is a specified file encoding.

Finally, summarize: Java must specify the encoding method of the data stream when you read the data stream (at least the String stream is read.

You may get relevant information through http://www.ibeyond.org/, you can also contact me via 9cbs@ibeyond.org.

The copyright of this article belongs to ibeyond, please indicate the reprint when reprint.

转载请注明原文地址:https://www.9cbs.com/read-31151.html

New Post(0)