File. encoding of system property in Java

Source: Internet
Author: User

Code

System.getProperty("file.encoding")

You can obtain the value of file. encoding.

Question 1: What is the value of file. encoding? At the beginning, I thought it was the operating system code.

But I am wrong. Its value is the storage encoding of the Java file that saves the main entry of each program (click the file --> attribute --> text file encoding (other value ), this is using the eclipse compiler)

The following code proves that:

/**
* Test. Java source code file
*/
Public class test {
Public static void main (string [] ARGs ){
System. Out. println (system. getproperty ("file. encoding "));
// Test2.printencoding ();
}
}

1. the encoding of the file is gb2312.

After compilation and execution, the result is:

GB2312 

2. the encoding of the file is GBK.

After compilation and execution, the result is:

GBK

3. the encoding of the file is: UTF-8

After compilation and execution, the result is:

UTF-8

The preceding three results show that the file. Encoding value is not the default Operating System OS encoding.

It is the storage encoding of the class where the main entry function is located.

But now I think of another problem,

Question 2: Does each Java file have its own file. encoding?

So I wrote another class for testing and added the class file test2.java.

/**
* Test2.java source code file
*/
Public class Test2 {
Public static void printencoding (){
System. Out. println (system. getproperty ("file. encoding "));
}
}

Modify the code of the source file test. Java:

/**
* Test. Java source code file
*/
Public class test {
Public static void main (string [] ARGs ){
System. Out. println (system. getproperty ("file. encoding "));
Test2.printencoding (); // added the command
}
}

At this time, I set the encoding of test. Java to UTF-8.

Set the storage encoding of test2.java to GBK.

According to question 2, my original assumption is:

After compiling and running these two files, the result should be:

utf-8GBK

  

However, the result after running is:

utf-8utf-8

The conclusion is as follows:

In a running application

File. encoding has only one value, and the value is the encoded value stored by the entry function.

 

As for the application of file. Encoding in code, we will discuss it later.

 

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.