java - BeanIo 无法读取特殊字符

标签 java unix encoding character-encoding bean-io

我有一个采用 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/

相关文章:

java - Log4j 登录到多个文件

linux - 文件说它在那里,但它根本不在那里

database - Spring 安全 :password encoding in DB and in applicationContext

java - 由于编码问题,Maven 未能通过我的单元测试

java - 如何向所有运行 Web 应用程序的服务器广播缓存无效消息?

java - Volley 请求重试策略不考虑超时

java - 将 ImageView 设置为 android fragment 中的 URL

linux - cd进入目录没有权限

bash - 将文件从一个位置复制到另一个位置并重命名的 Shell 脚本将当前日期添加到每个文件

json - Golang json解析