IO:如何使用FileReader来读取文件:
package net.nyist.io;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args){
try(
//创建字符输出流,使用java7新特性,自动关闭资源
FileReader fr = new FileReader("src/net/nyist/io/FileReaderTest.java");
){
//创建一个长度为32的字符数组,数组不够大,需要多次读取,但是不会出现中文注释乱码问题
char[] cbuf = new char[32];
//用于保存实际读取的字符数
int hasRead = 0;
//使用循环读取数据
while((hasRead = fr.read(cbuf)) > 0){
//将字符数组转化为字符串输出
System.out.println(new String(cbuf,0,hasRead));
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}