java FileReader 的 read()方法

用read()方法读取文件中的字符返回为int类型,输出后全为数字,那些数字是什么?重要的是,如何能使输出变成读取的原字符?
例如 : FileReader file = new FileReader("test.txt");
int test = 0;
while ((test = file.read()) != -1)
{
System.out.print(test);
}
“test.txt”=“abc123456defg”
输出为全看不懂的数字,如何让输出变成“abc123456defg”。

FileReader类的read函数有两个重载形式。
一个即题目中的:

 public int read() //读取单个字符,返回值为int类型,代表该字符的ascii码值

另一个是:

 public int read(char [] c, int offset, int len) //读取若干字符到字符数组中,返回值为读取的字符数

对题目而言,可以用第二种重载形式,参考如下范例:

      FileReader fr = new FileReader("test.txt"); 
      char [] a = new char[50];
      fr.read(a);   // reads the content to the array

      for(char c : a)
         System.out.print(c);   // prints the characters one by one
      fr.close();

如果对您有帮助,请采纳答案好吗,谢谢!

http://blog.csdn.net/liuhenghui5201/article/details/8276278

read
public int read()
throws IOException读取单个字符。在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
用于支持高效的单字符输入的子类应重写此方法。

返回:
作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误


read
public int read(char[] cbuf)
throws IOException将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。

参数:
cbuf - 目标缓冲区
返回:
读取的字符数,如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误


read
public abstract int read(char[] cbuf,
int off,
int len)
throws IOException将字符读入数组的某一部分。在某个输入可用、发生 I/O 错误或者到达流的末尾前,此方法一直阻塞。

参数:
cbuf - 目标缓冲区
off - 开始存储字符处的偏移量
len - 要读取的最多字符数
返回:
读取的字符数,如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误

System.out.print(test);改为

System.out.print((char)test);