The Chinese patterns in the JAVA_I/O reading

Source: Internet
Author: User

There's not that much nonsense. direct to the point! For reading local files and changing the UTF8 format is still a part of the process of garbled, remedial or correct posture. Look at the regular reading first (at least my first reaction is to say that ...)

1     //if error, throw IO exception in console2      Public StaticString read (string filename)throwsioexception{3BufferedReader in=NewBufferedReader (Newfilereader (filename));4 String S;5StringBuilder sb=NewStringBuilder ();6         //Read one line at a time until a null is encountered and the file is read to the end.7          while((S=in.readline ())! =NULL){8             //easy to read Chinese garbled, so set UTF89String str=NewString (S.getbytes (), "UTF-8");Ten             //each time you finish reading a newline character. OneSb.append (str+ "\ n"); A         } -         //Close the stream - in.close (); the         returnsb.tostring (); -}

It looks so big on the face. A few lines of code read the file in minutes. [String can use list ah no need to change the line.], but the problem comes ... Write a file, just a word. Let's read it and see.

1 1. // This is a 1 GE file read Test 2 2. // The purpose is to Ceshi files . 3 3. So normal printing is for the better ... 4 4. What?

This is the test print content, see output

  

Oh, my God. What's the situation ... A wordy look:

The problem is that in the process of FileReader reading a file, FileReader inherits InputStreamReader, but does not implement a constructor with character set parameters in the parent class, so filereader can only be decoded by the system's default character set. Then the code loses during the reading of the UTF-8, resulting in the result being unable to restore the original character. is the question mark.

Know why ... Well, how about that, huh? Just use InputStreamReader. #| BufferedReader in=new BufferedReader (New InputStreamReader (new FileInputStream (filename), "UTF-8")) |# na ~ so okay

1     //if error, throw IO exception in console2      Public Staticlist<string> readlist (String filename)throwsioexception{3BufferedReader in=NewBufferedReader (NewInputStreamReader (NewFileInputStream (filename), "UTF-8"));4 String S;5List<string> ll=NewArraylist<string>();6         //Read one line at a time until a null is encountered and the file is read to the end.7          while((S=in.readline ())! =NULL){8 Ll.add (s);9         }Ten         //Close the stream One in.close (); A         returnll; -}

okay~ so the problem is solved ... In fact, there is a more simple method ... When you save the file code, you UTF-8 directly ... wow haha haha ~ but not always thinking about this, in case you forget to insure a little.

_ Leaves Falling stars

@ Acknowledgements Iteye's gundumw100 explains why the code was lost.

The Chinese patterns in the JAVA_I/O reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.