Java File Encoding issues

Source: Internet
Author: User

1 , Try {

The default current directory for Eclipse is the project root, and the relative path used in new File () should be the path to the relative root directory

FileInputStream input=new FileInputStream (New File ("Bin\\cn\\google\\demo\\data.txt"));

Byte[] B=new byte[100];

int Len=input.read (b);

Solve garbled problems

Method One: Manually construct string decoding

String Str=new string (B,0,len, "UTF-8");

System.out.println (str);

Method Two: Convert the byte stream to the corresponding coded character stream using the InputStreamReader object

FileInputStream input=New fileinputstream (new File ("Bin\\cn\\google\\demo\\data.txt"));

BufferedReader reader=New BufferedReader (new inputstreamreader(input, "UTF-8"));

int A;

while ((A=reader.read ())!=-1) {

System. out. Print ((char) a);

}

} catch (FileNotFoundException e) {

E.printstacktrace ();

}

2, System.out and system.in are coded output and input according to the platform's default code table, using the new string (...) to set the Code table in the parameters can be decoded, using the String's GetBytes (...) method to encode characters.

3. The properties of the project in Eclipse are encoded only by the Code table in the eclipse environment, that is, simply changing the encoding of system.in and System.out

Java File Encoding issues

Related Article

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.