简体   繁体   English

始终在Java中设置file.encoding系统属性是一个好主意吗?

[英]Is it a good idea to always set file.encoding system property in Java?

I think it is a good idea to always set file.encoding system property in Java application. 我认为始终在Java应用程序中设置file.encoding系统属性是个好主意。

Suppose I do not set file.encoding . 假设我没有设置file.encoding It means that Java will use a platform-dependent default charset (eg in String.getBytes ), which makes the whole application platform-dependent. 这意味着Java将使用依赖于平台的默认字符集(例如,在String.getBytes ),这使得整个应用程序依赖于平台。

If we set -Dfile.encoding=UTF-8 , for example, we guarantee that such calls as String.getBytes work the same in any platform. 例如,如果我们设置-Dfile.encoding=UTF-8 ,我们保证像String.getBytes这样的调用在任何平台上都是一样的。

Does it make sense? 是否有意义?

No, it doesn't necessarily make sense. 不,它不一定有意义。 If you want to read files that have not been created by your own application, on any platform, you'd better leave the file encoding as it is by default, because that's what you'll need to be able to read these files. 如果您想在任何平台上读取未由您自己的应用程序创建的文件,您最好保留默认情况下的文件编码,因为这是您需要能够读取这些文件的内容。

And if you read files created by your own applications, or by applications which use a well-known and specified file encoding, then you should simply use this encoding when instantiating your IO readers and writers. 如果您读取由您自己的应用程序创建的文件,或者使用已知和指定的文件编码的应用程序,那么您应该在实例化IO读取器和编写器时使用此编码。

For methods such as String.getBytes() just don't use them, and use String.getBytes(Charset) instead if you want to use a specific encoding instead of the platform's default one. 对于诸如String.getBytes()方法,只是不要使用它们,如果要使用特定的编码而不是平台的默认编码,请使用String.getBytes(Charset)

Conditionally yes. 有条件的是。 As JB mentioned, using the "platform default" may occasionally help when reading files generated by other local applications (or other remote ones on the same platform if you have a homogeneous server farm). 正如JB所提到的,当读取由其他本地应用程序(或同一平台上的其他远程应用程序,如果您有同构服务器场)生成的文件时,使用“平台默认”可能偶尔会有所帮助。

So, take the choice with care, but in general I'd say do it. 因此,谨慎选择,但总的来说,我会说这样做。 The advice to always create your own readers isn't always possible. 总是创建自己的读者的建议并不总是可行的。 I believe in general that most things generating files that use extended characters end up using UTF-8 for it. 我相信一般来说,生成使用扩展字符的文件的大多数事情最终会使用UTF-8。

In the end, because many files are dependent on the choices made outside your control, it's going to come down to testing and customization, but I feel far more comfortable suggesting that you start with UTF-8 and downgrade as necessary than the inverse. 最后,因为许多文件都依赖于你控制之外做出的选择,所以它将归结为测试和自定义,但是我觉得你更开心地建议你从UTF-8开始并根据需要降级而不是反向。

It is not generally a good idea to set the file.encoding System-Property, because this is not a supported configuration option in Java. 设置file.encoding System-Property通常不是一个好主意,因为这不是Java中支持的配置选项。

That means it may or may not work. 这意味着它可能会也可能不会起作用。 Not working can mean Exceptions . 不工作可能意味着例外 To be precise problems of the sort "It works on Java 1.6, it works on Java 1.7 on Windows, BUT it does not work on Java 1.7 on Linux anymore." 确切地说,“它适用于Java 1.6,它适用于Windows上的Java 1.7,但它不再适用于Linux上的Java 1.7”。

The reason behind that is given here : 这背后的原因在这里给出:

The "file.encoding" property is not required by the J2SE platform specification; J2SE平台规范不要求“file.encoding”属性; it's an internal detail of Sun's implementations and should not be examined or modified by user code. 它是Sun实现的内部细节,不应由用户代码检查或修改。 It's also intended to be read-only; 它也是只读的; it's technically impossible to support the setting of this property to arbitrary values on the command line or at any other time during program execution. 在技​​术上不可能支持在命令行或程序执行期间的任何其他时间将此属性设置为任意值。

The preferred way to change the default encoding used by the VM and the runtime system is to change the locale of the underlying platform before starting your Java program. 更改VM和运行时系统使用的默认编码的首选方法是在启动Java程序之前更改底层平台的区域设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM