java字符串转换整型 Integer.parseInt()遇到java.lang.NumberFormatException: For input string: "0" ,最终发现是编码问题,带有非法字符'\uFEFF'

start = Integer.parseInt(ss.trim());
cnt = Integer.valueOf(cc.trim());

java.lang.NumberFormatException: For input string: "0"

代码是从一个 txt文本读取一行 如 0,7 的拆分进行int转换,结果报错,百度了很久,百思不得其姐,调试看得到的 也是标准字符串“0” ,

点开value 万万没有想到前面还有个非法字符。

        List<String> lines = null;
        lines = FileUtils.readLines(new File("Log\\22222.txt") );for (String line : lines) {
            int start = 0,cnt = 0;
            String ss=line.split(",")[0];
            String cc=line.split(",")[1];
            start = Integer.parseInt(ss.trim());//报错java.lang.NumberFormatException: For input string: "0"
            cnt = Integer.valueOf(cc.trim());
        }

调试看下方:读到的 “0,7”   的value前面还有个非法占位符 (在外面看不出有空格和什么占位符) 

 用nodepad++ 看所有字符也看不出

 

背景:本人原来的txt是由如下代码生成的,

FileUtils.writeStringToFile(new File("Log\\22222.txt"),  "****\n", "utf-8", true);
保存的格式估计就不是 电脑手动生成的一样了。所以txt保存的时候也有多种格式,容易出问题。


 最后 重新建立新的  txt 文档,再读取就没有 非法字符'\uFEFF'在前面了,



网上有很多是直接解决 java: 非法字符: ‘\ufeff’ 的帖子 可以看看。 我直接创建新的 文本解决了。就没有折腾了。主要调试的时候要点进去看value。
posted @ 2022-08-08 09:24  海蓝7  阅读(836)  评论(0编辑  收藏  举报