简体   繁体   中英

Why the program is printing something else when reading from a file?

I am new to java and now I am learning streams. I have this code:

Code:

FileReader fr = new FileReader("C:\\Users\\Lacatin\\Desktop\\test.txt");
BufferedReader r = new BufferedReader(fr);
int n = r.read();
System.out.println(n);

I am trying to read a matrix from a text file(.txt), n representing the number of lines which is 3, but when I run the code it shows 51 and I don't know why. Can someone help me please?

Colde block

File content

The BufferedReader read method reads one character as an integer, the numerical representation of a character, the integer associated to the number 3 is the character 51. You can see a list here: http://www.asciitable.com/

If you wan exactly the string then you can use readLine, this returns the String representation of one line, if you want to read the whole file line by line you need to do a while loop while the line is not null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM