简体   繁体   English

E / System:忽略尝试将属性“ file.encoding”设置为值“ ISO-8859-1”

[英]E/System: Ignoring attempt to set property “file.encoding” to value “ISO-8859-1”

When i try to this in Android Studio it ignores the encoding resulting a disaster in my program.But it does not have any problem when i try this on java. 当我在Android Studio中尝试此操作时,它会忽略导致程序崩溃的编码。但是在Java上尝试此操作时没有任何问题。

System.setProperty("file.encoding","ISO-8859-1");
            Field charset = Charset.class.getDeclaredField("defaultCharset");
            charset.setAccessible(true);
            charset.set(null,null);

on the log it displays: 在日志上显示:

E/System: Ignoring attempt to set property "file.encoding" to value "ISO-8859-1" E / System:忽略尝试将属性“ file.encoding”设置为值“ ISO-8859-1”的尝试

Setting a system property can be prevented by a SecurityManager if one is installed. 如果安装了SecurityManager,则可以阻止设置系统属性。 An Android application can be expected to run in a sandbox which means that there is a SecurityManager in place that resticts the system properties you can set (there might be some but don't count on it). 可以预期Android应用程序将在沙箱中运行,这意味着有一个SecurityManager可以限制您可以设置的系统属性(可能有一些但不依赖它)。

A regular Java application in general runs without a SecurityManager so setting this property works. 通常,常规Java应用程序在没有SecurityManager的情况下运行,因此可以设置此属性。

Normally setting the file.encoding during runtime isn't necessary. 通常不需要在运行时设置file.encoding。 If your application breaks without a specific value for file.encoding you most likely do something wrong within your code, eg creating a String froma byte[] without specifying the charset to be used or vice versa. 如果您的应用程序在没有特定的file.encoding值的情况下中断,则很可能在代码内发生了一些错误,例如,从byte[]创建一个String而不指定要使用的字符集,反之亦然。

In short: In order to get your application to work you need to change your application from eg 简而言之:为了使您的应用程序正常工作,您需要从以下位置更改应用程序:

byte[] myBytes = myString.getBytes();
String mynewString = new String(myBytes);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));

to

byte[] myBytes = myString.getBytes("8859_1");
String mynewString = new String(myBytes, "8859_1");
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "8859_1");

Oh and something more: 哦,还有更多:

Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);

This is quite a hack and you should feel dirty for that ;-) This won't solve all problem, eg when you do HTTP-requests, file.encoding is used as well to decide what charset should be used to encode HTTP-request-header-values. 这是一个很不错的技巧,您应该为此感到肮脏;-)不能解决所有问题,例如,当您执行HTTP请求时,也使用file.encoding来决定应使用哪种字符集来编码HTTP请求-header-values。 The charset value for that is kept in a different member of a different class, same for similar functionalities in JavaMail, etc. Changing the charset-values in members of "internal" classes is quite likely breaking things, so don't do that and as I already wrote, it should be completely unnecessary. 该字符集的值保留在不同类的不同成员中,对于JavaMail中的类似功能等也是如此。更改“内部”类的成员中的字符集值很可能会破坏事情,因此请不要这样做,并且正如我已经写过的,应该完全没有必要。

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

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