Why is my BufferedReader reading text that dosn't exit in the given file?

对着背影说爱祢 提交于 2019-12-02 07:42:28

问题


I am using a BufferedReader to read details from a file which are stored as bytes, I am then converting the bytes into text and splitting it into an array. However my program is returning a NumberFormatException from the text read from the file. Below I have provided my code, the error message, the line read from the file and the print statement used to show what the BufferedReader has read from the file.

FileReader fileReader = new FileReader("VirtualATM.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
line = bufferedReader.readLine();
System.out.println(line);
String line = line.substring(1, line.length()-1);   //Convert bytes read back to String
StringBuilder sb = new StringBuilder();
for(String s: line.split(", ")) //Split every byte on ,
sb.append((char) Integer.parseInt(s));  //Convert byte to char
String text = sb.toString();    //Convert StringBuilder to String
String [] cardNum = text.split("\\s+"); //Split line read from file on every blank space into an array

Line in the text file that is being read:

[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 54, 55, 56, 56, 55, 53, 55, 49, 57, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 51, 55, 48, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 53, 50, 53]

Line from print statement - System.out.println(line):

[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 54, 55, 56, 56, 55, 53, 55, 49, 57, 32, 67, 117, 114, 114, 101, 110, 116, 32, 6 6, 97, 108, 97, 110, 99, 101, 32, 51, 55, 48, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 1 02, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 53, 50, 53]

Exception thrown:

Exception in thread "main" java.lang.NumberFormatException: For input string: "»¿[78"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)

The exception, as you can see is thrown because of the characters  which are appearing when the bufferedReader reads from a file (even though they don't exist in the file). If anyone could help me figure out where these characters are coming from and why they are appearing, that would be great!


回答1:


What you see is the UTF-8 BOM

Convert your input file to be without BOM.




回答2:


Follow this code:

        String lineFromFile = bufferedReader.readLine();
        // strip out the `[` and `]`
        lineFromFile = lineFromFile.substring(1, lineFromFile.length()-1);
        StringBuilder sb = new StringBuilder();
        for(String s: lineFromFile.split(", "))
            sb.append((char) Integer.parseInt(s));
        String text = sb.toString();



回答3:


white spaces may cos this error. try to trim() the input line while reading line,

like,

line = bufferedReader.readLine().trim();


来源:https://stackoverflow.com/questions/27618371/why-is-my-bufferedreader-reading-text-that-dosnt-exit-in-the-given-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!