Java file_encoding Properties

Source: Internet
Author: User

Today to the customer issued a version number, suddenly found that the report export content is empty, size 0 bytes. It is strange to think that the development of the time is good, packaging out how the exception. After a closer look, it is file_encoding this Java System attribute encoding mode settings. At the time of development we generally set the project's text file encoding to Utf-8 in Eclipse. :


After the development, we need to specify the code to run the Java program after leaving Eclipse, otherwise when you output System.getproperty ("file.encoding") this property, the result is the system default encoding method,

Under Windows is usually GBK. The specified encoding is also very easy, java-dfile.encoding=utf-8 xxxx (required to run the class file)

The following is a look at the English explanation of the File.encoding property.

Used for the default encoding in Java, all readers and writers would default to use thi S property. "File.encoding" is a set to the default locale of the Windows operationg system since Java 1.4.2. System.getproperty ("file.encoding") can is used to access the property. Code such as System.setproperty ("file.encoding", "UTF-8") can is used to the change the property. However, the default encoding can is changed dynamically even this property can be changed. So the conclusion was that the default encoding can ' t was changed after JVM starts. "Java-dfile.encoding=utf-8" Can is 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 meaning is mainly the following points:

1. All reader and writer operations in Java are by default file.encoding this system attribute as the encoding method, see the code:

Way1string html1= "



The first method encodes the file by default with the File.encoding property, and then outputs it. Once you run the class file without specifying the attribute, the default is to encode the operating system itself, such as GBK.

Another way to specify the file encoding, and output.

I encountered an exception in the project is caused by the first method, just started I used another way to solve, but this can only solve this place, other not found is not good to conquer. A better solution, see note point 2.

2.JVM before starting to assume that the file.encoding attribute is not specified, this property will be assumed that the operating system encoding, the JVM startup assumes that the File.encoding attribute is specified, the entire project will use this property

As the default encoding for reader and writer operations.

So, the best way to solve this problem is to know the File.encoding property when you start the project, and maybe the read and write operation has no special coding needs, and can inherit from it.




Java file_encoding Properties

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.