13- IO流 ---- 节点流(或文件流)(2)FileReaderWriterTest①使用FileReader读入数据的两种操作
将hello.txt文件内容读入程序中,并输出到控制台
//2.提供具体的流
fr = new FileReader(file);
//3.数据的读入过程
//read():返回读入的一个字符,如果达到文件末尾,返回-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);
}
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次对如cbuf数组中的字符的个数,如果达到文件末尾,返回-1
char[] cbuf = new char[3];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//错误的写法,对应着方式一的错误的写法
// String str = new String(cbuf);
// System.out.println(str);
//正确的写法
String str = new String(cbuf,0,len);
System.out.print(str);
}
说明点:
1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
3. 读入的文件一定要存在,否则就会报FileNotFoundException。
代码:
package java2;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
File file = new File("hello.txt");//相较于当前工程
System.out.println(file.getAbsolutePath());
File file1 = new File("day09\\hello.txt");
System.out.println(file1.getAbsolutePath());
}
//将hello.txt文件内容读入程序中,并输出到控制台
@Test//实现了读取系统文件的能力
public void testFileReader() {
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");//相较于当前Module
System.out.println(file.getAbsolutePath());
//2.提供具体的流
fr = new FileReader(file);
//3.数据的读入过程
//read():返回读入的一个字符,如果达到文件末尾,返回-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 (IOException e) {
e.printStackTrace();
} finally {
//4.流的关闭操作(必须要写)
//方式一:
// try {
// if (fr != null){
// fr.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
//方式二
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//对read()操作升级:使用read的重载方法
@Test
public void testFileRead1(){
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次对如cbuf数组中的字符的个数,如果达到文件末尾,返回-1
char[] cbuf = new char[3];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//错误的写法,对应着方式一的错误的写法
// String str = new String(cbuf);
// System.out.println(str);
//正确的写法
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源的关闭
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
main输出:
D:\workspace\idea1\JavaSenior\hello.txt
D:\workspace\idea1\JavaSenior\day09\hello.txt
testFileReader输出:
D:\workspace\idea1\JavaSenior\08IOliu\hello.txt
helloworld
testFileReader1输出:
helloworld