FileReader读入数据的基本操作
read():每次读入一个字符
读入的文件一定要存在,否则会报FileNotFound的异常
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test {
//将文件中的数据读入到程序中,并输出到控制台
public static void main(String[] args) {
FileReader fr=null;//记得放在外面,如果放在try中那finally作用域内,用不了
try{
//注意:如果写成File f=new File("hello.txt");这种相对路径是针对当前工程而言的
//1.实例化File类的对象,指明要操作的文件
File f= new File("test\\hello.txt");//这种写法是test这个module下的hello.txt
//2.提供具体的流//3.数据的读入过程
fr=new FileReader(f);//如果f对应的文件不存在,这里一定会抛异常
//read():返回读入的一个(注意只会读一个)字符,是以int的方式存的char类型。如果达到文件末尾,返回-1
//方式一
//int data=fr.read();
//while(data!=-1){
// System.out.print((char) data);
// data=fr.read();
//}
//方式二:效率是一样的
int data;
while((data=fr.read())!=-1){
System.out.print((char)data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
//4.流的关闭操作,千万别忘,不会自动关闭,需要我们手动关闭
if(fr!=null)//如果是在fr=new FileReader(f);这里出的异常,那么这一句就没有执行,又因为fr是null,所以对象没有实例化,执行finally会出空指针异常
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
关于本程序中异常的处理,read(),close(),new FileReader(f)都可能会抛出异常,不能采取throws的方式处理,因为假设new FileReader(f)
正常执行,但在执行read()处发生阻塞,无法读取,抛出了异常,那么之后的程序就都不会执行了,那流就没有关闭
所以一定要用try的方式解决,而且要把close()放到finally当中。
又因为close自己还会抛出异常,再加上try
*/
/*
上面一小段变成这样也可以
if(fr!=null){
try{
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
read方法的操作升级:使用read的重载方法
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* read的读入操作的升级
* read():表示每次读入的是一个字符,如果文本文件有10个字符要读11次,因为最后一次要读到-1表示读到末尾,当数据量大时这样效率低,因为要多次和硬盘进行交互
* 升级操作:制造char型的数组给read(因为现在读的是字符,如果读的是字节,要用byte型数组)
* 注意:read还有一种版本但通常我们不会去用,read(char[] b,int off,int len);
* 比如read(cbuf,0,3);假设cbuf的长度是5,这里是每次从头开始读,每次读三个
*/
public class Test {
//将文件中的数据读入到程序中,并输出到控制台
public static void main(String[] args){
FileReader fr=null;
try{
//1.File类的实例化
// File f=new File("test\\hello.txt");
//System.out.println(f.getAbsolutePath());//C:\Users\11930\IdeaProjects\project01\test\hello.txt,文件是不存在的,因为是在src中存在的
File f=new File("test\\src\\hello.txt");//一定要这么写才可以
System.out.println(f.exists());//返回true
//2.流的实例化(因为操作的是文本文件,所以用的是FileReader)
fr=new FileReader(f);
//3.读入的具体操作细节
char[] cbuf=new char[5];//也就是说每次读入最多读5个字符
//read此时返回该次读到char数组中的字符个数,比如文件内容为abcdefg。第二次返回的值为2,读到文件的末尾还是返回-1
int len;
while((len=fr.read(cbuf))!=-1) {
/*方式一
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
}
*/
//方式二:用char型数组到String的转换,即把字符数组当中的数据变化成一个字符串了,可以不取字符数组中的所有数据
//可以从头开始取,每次取len个,写成String str=new String(cbuf,0,len);
//错误写法: String str=new String(cbuf);
//错误写法:System.out.print(str);//这么干的结果又是helloworld123ld
String str=new String(cbuf,0,len);
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
//4.流资源的关闭(如果涉及到多个也都关掉)
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*数据的不同会导致步骤2,3的实现不同,但这些步骤的执行顺序都是一样的
如果文件中的内容为helloworld123,for循环为for(int i=0;i<cbuf.length;i++){System.out.print(cbuf[i]);}
输出的结果为:helloworld123ld,第三次len为3,执行第二次之后数组内容为world,第三次读入123,只是覆盖了前三个字符
ld没有变,所以数组为123ld,.length的结果为5,所以这么写是错误的写法
*/