---
title: "包含中文的properties文件，第一行要空出来"
author: "asjava"
date: "2017-04-17T16:04:50Z"
tags: []
categories: ["编程语言"]
slug: "properties"
draft: false
lastmod: "2017-04-17T16:04:50Z"
id: 2369679
url: "https://asjava.iteye.com/blog/2369679"
---

项目的配置文件中包含了中文，文件的编码格式为UTF-8.

当读取properties文件时第一个Key总是失败，后面的Key则正常。

Properties类API([http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html))中提到如下一句话

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The [Java](http://lib.csdn.net/base/javase "Java SE知识库")™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

大体意思是, properties文件是基于行来读取和存储键值对的, 而期望的输入/输出流字符编码是ISO 8859-1. 那些不能使用该方式存储的字符集呢, 需要使用native2ascii命令对property文件进行转码.

解决方案为：在properties文件的首先加为空行（或写注释也可以）

参考：http://blog.csdn.net/pansanday/article/details/49895399