boolean ready()
讲解:判断此流是否已准备好被读取,依赖其他流,所以一般需要做判断
int read()
讲解:读取单个字符
int read(char[] cbuf, int off, int len)
讲解:读取一部分字符到数组里面,从数组下标off处放置length长度的字符
String readLine()
讲解:读取一整行文本行,返回一整行字符串,如果读到行尾了就返回null,注意返回的一行字符 中不
包含换行符
void close()
讲解:关闭流释放资源
代码:
public class chapter11_3 {
public static void main(String[] args)throws Exception{
test2("C:\\Users\\Administrator\\Desktop\\test\\3.txt");
}
public static void test1(String path)throws Exception{
File file1=new File(path);
Reader reader=new FileReader(file1);
BufferedReader bufferedReader=new BufferedReader(reader);
if (!bufferedReader.ready()){
System.out.println("文件暂时无法读取");
return;
}
int size;
char[] buf=new char[1024];
while ((size=bufferedReader.read(buf,0,buf.length))!=-1){
System.out.println(new String(buf,0,size));
}
bufferedReader.close();
}
//一行行读取
public static void test2(String path)throws Exception {
File file1 = new File(path);
Reader reader = new FileReader(file1);
BufferedReader bufferedReader = new BufferedReader(reader);
if (!bufferedReader.ready()){
System.out.println("文件暂时无法读取");
return;
}
String name="";
while ((name=bufferedReader.readLine())!=null){
System.out.println(name);
}
bufferedReader.close();
}
}
效果截图: