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.