【问题标题】:Reading from property file containing utf 8 character从包含 utf 8 字符的属性文件中读取
【发布时间】:2020-07-25 09:02:56
【问题描述】:

我正在读取一个包含 UTF-8 字符集中消息的属性文件。

问题

输出的格式不正确。我正在使用InputStream

属性文件看起来像

username=LBSUSER
password=Lbs@123
url=http://localhost:1010/soapfe/services/MessagingWS
timeout=20000
message=Spanish character are = {á é í, ó,ú ,ü, ñ, ç, å, Á, É, Í, Ó, Ú, Ü, Ñ, Ç, ¿, °, 4° año = cuarto año, €, ¢, £, ¥}

我正在读这样的文件,

Properties props = new Properties();
props.load(new FileInputStream("uinsoaptest.properties"));
String username = props.getProperty("username", "test");
String password = props.getProperty("password", "12345");
String url = props.getProperty("url", "12345");
int timeout = Integer.parseInt(props.getProperty("timeout", "8000"));
String messagetext = props.getProperty("message");
System.out.println("This is soap msg : " + messagetext);

上述消息的输出是

可以在控制台中看到该行后面的消息

{************************ SOAP MESSAGE TEST***********************}

如果我能获得正确阅读此文件的任何帮助,我将不胜感激。我可以用另一种方法读取这个文件,但我正在寻找更少的代码修改。

【问题讨论】:

    标签: java parsing encoding utf-8


    【解决方案1】:

    使用InputStreamReaderProperties.load(Reader reader)

    FileInputStream input = new FileInputStream(new File("uinsoaptest.properties"));
    props.load(new InputStreamReader(input, Charset.forName("UTF-8")));
    

    作为一种方法,这可能类似于以下内容:

      private Properties read( final Path file ) throws IOException {
        final var properties = new Properties();
    
        try( final var in = new InputStreamReader(
          new FileInputStream( file.toFile() ), StandardCharsets.UTF_8 ) ) {
          properties.load( in );
        }
    
        return properties;
      }
    

    不要忘记关闭您的信息流。 Java 7 引入了StandardCharsets.UTF_8

    【讨论】:

    • 这是一个奇怪的问题。在 ubuntu 中我只使用 props.load(propFile) 并且效果很好,但在 Windows 中我必须使用上面的代码才能正确加载 unicode 道具文件!
    • 毕竟这并不奇怪。字符集严重依赖于平台。因此,Ubuntu 的行为与 Windows 不同也就不足为奇了。事实上,在迁移到下一个 Windows 版本(或者准确地说:另一个 JVM 版本)之后,您可能需要再次更改代码。
    • FileInputStream inputStream=new FileInputStream(new File(propFileName) throws FileNotFoundException.propFileName 是 config/cop-app-dev.properties
    • @MukhamedaliZhadigerov 这是另一个问题。确保具有属性的文件相对于您的 java 可执行文件存在(并且可读)。如果不确定,请尝试绝对路径。
    • @Würgspaß 是的。它已呈现且可访问。我可以使用InputStream inputStream=getClass().getClassLoader().getResourceAsStream(propFileName)properties.load(inputStream); 访问该属性文件
    【解决方案2】:

    请改用props.load(new FileReader("uinsoaptest.properties"))。默认情况下,它使用编码Charset.forName(System.getProperty("file.encoding")),可以使用System.setProperty("file.encoding", "UTF-8") 或命令行参数-Dfile.encoding=UTF-8 设置为UTF-8。

    【讨论】:

    • 这在 Windows 中的 Java 8 上不起作用。如果您查看 Java 源代码,它会硬编码为 load0(new LineReader (new InputStreamReader(bis, "ISO8859-1")));
    【解决方案3】:

    如果有人使用@Value 注解,可以试试 StringUils。

    @Value("${title}")
    private String pageTitle;
    
    public String getPageTitle() {
        return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8"));
    }
    

    【讨论】:

      【解决方案4】:

      您应该在构造 FileInputStream 对象时指定 UTF-8 编码。你可以使用这个构造函数:

      new FileInputStream("uinsoaptest.properties", "UTF-8");
      

      如果您想更改您的 JVM 以便能够默认读取 UTF-8 文件,您必须将 JVM 选项中的 JAVA_TOOL_OPTIONS 更改为如下内容:

      -Dfile.encoding=UTF-8
      

      【讨论】:

      • 这个构造函数不存在。
      • @JavaFanatic 是的,检查构造函数,可能是你要写别的东西..
      • @Binkan Salaryman FileInputStream.<init>(String, String) 是什么?我不明白
      • @MukhamedaliZhadigerov <init> 是构造函数的反射内部方法名称,所以这是指class FileInputStream 的构造函数,参数类型为StringString
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 2012-02-06
      • 1970-01-01
      相关资源
      最近更新 更多