我有一个采用 ANSI (windows-1252) 编码的 .txt 文件,我可以在 Windows 上读取它,但不能在 unix 上读取。
这是 xml 映射文件:
<beanio> <stream name="empData" format="csv"> <parser> <property name="delimiter" value=";"/> <property name="alwaysQuote" value="false"/> <!--<property name="quote" value='' />--> </parser> <record name="emp" class="com.MyClass" > <field name="name" /> <field name="job" /> <field name="adress"/> </record> </stream> </beanio>
Java 端:
StreamFactory factory = StreamFactory.newInstance();
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("mapping.xml");
Reader reader = new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream("countries.txt"));
factory.load(in);
BeanReader beanReader = factory.createReader("empData", reader);
Gson gson = new Gson();
/*Object bean =new Object();*/
Object record = null;
while ((record = beanReader.read()) != null) {
System.out.println(beanReader.getRecordName() + ": "
+((MyClass)record).getCountry());
}
结果: 线路:法国
line : S??o Paulo 应该是(圣保罗)windows 可以,但 unix 不行
线路:美国
线路:中国
有什么想法吗?
仅供引用:我已经尝试将 Charset 设置为 UTF-8 java 端。
new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("clearings.txt"), Charset.forName("UTF-8"));
最佳答案
有点晚了,但是...如果您有一个 ANSI 文件,即 ISO-8859-1
,则必须将 InputStreamReader
字符集设置为 ISO-8859-1,而不是 UTF-8。
Reader reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("clearings.txt"), StandardCharsets.ISO_8859_1);
关于java - BeanIo 无法读取特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43685583/