简体   繁体   English

为什么程序在从文件读取时打印其他内容?

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

I am new to java and now I am learning streams.我是 java 的新手,现在我正在学习流。 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.我正在尝试从文本文件(.txt)中读取矩阵,n 表示行数为 3,但是当我运行代码时它显示 51,我不知道为什么。 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/ BufferedReader读取方法读取一个字符为integer,一个字符的数值表示,与数字3关联的integer是字符51。可以在这里查看一个列表: Z80791B3AE7002CB888C2466.com/8C246

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.如果你想要字符串,那么你可以使用 readLine,这将返回一行的字符串表示,如果你想逐行读取整个文件,你需要执行一个 while 循环,而该行不是 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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