Java File_encoding attribute, javafile_encoding

Source: Internet
Author: User

Java File_encoding attribute, javafile_encoding
Today, I sent a version to the customer and suddenly found that the report export content is empty, with a size of 0 bytes. it is very strange, because the development process is good, how can an exception occur when it is packaged. the result is that file_encoding is the java System attribute encoding method. during development, we usually set the project's text file encoding attribute to UTF-8 in eclipse.:


After development, we also need to specify the encoding method to execute the java program. Otherwise, when you output the System. getProperty ("file. the default encoding method is used,

In windows, it is also very easy to specify the encoding method for GBK. java-Dfile. encoding = UTF-8 xxxx (class file to be executed)

Next, let's take a look at the English explanation of the attribute file. encoding.

This property is used for the default encoding in Java, allReadersAndWritersWocould default to use this property. "file. encoding "is set to the default locale of Windows operationg system since Java 1.4.2. system. getProperty ("file. encoding ") can be used to access this property. code such as System. setProperty ("file. encoding "," UTF-8 ") can be used to change this property. however, the default encoding can not be changed dynamically even this property can be changed. so the conclusion is that the default encoding can't be changed after JVM starts. "java-Dfile. encoding = UTF-8 "can be used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. but I can't find it.


The general meanings are as follows:

1. By default, all reader and writer operations in java use the system attribute file. encoding As the encoding method. See the Code:

//way1String html1="



The first method uses file by default. the encoding attribute is used to encode the file and then output the file. once this attribute is not specified when you execute the class file, the operating system encoding method, such as gbk, is used by default.

The second method specifies the file encoding method and outputs.

Even exceptions encountered in the project are caused by the first method. At first, I used the second method to solve the problem, but this can only solve this problem, other issues cannot be solved. for better solutions, see note 2.

2. if file is not specified before JVM is started. encoding, which defaults to the operating system encoding mode. If file is specified during JVM startup. this attribute is used by the entire project.

The default encoding method for reader and writer operations.

So, the best way to solve the problem is to know the file. encoding attribute when the project is started. Subsequent read/write operations can be inherited and used without any special encoding.





In Java, SystemgetProperty ("fileencoding") obtains the encoding attribute of the system. What is the value range of the text?

Is it to take all attribute values?
Java. lang. System

Static PropertiesgetProperties ()

Determines the current system properties.
 
Chinese garbled java files

Select your file -- Right-click -- properties -- text file encoding there is a default and other here select other and then select UTF-8 (your default may be ISO-8859-1)
Then you change the garbled characters in the file back to Chinese and save it ~ This time, no garbled characters ~

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.