FileReader、FileWriter

来源:互联网 发布:电脑上写小说的软件 编辑:程序博客网 时间:2024/05/24 05:31
字节流:字节流读取的是文件中的二进制数据,读到的数据并不会帮你转换成你看得懂的字符。 

 字符流: 字符流会把读取到的二进制的数据进行对应 的编码与解码工作。   字符流 = 字节流 + 编码(解码)


 输入字符流:

----------| Reader 输入字符流的基类   抽象类
-------------| FileReader 读取文件的输入字符流。

FileReader的用法:
1. 找到目标文件
2. 建立数据的输入通道
3. 读取数据

4. 关闭资源

public class Demo2 {public static void main(String[] args) throws IOException {readTest2();}//使用缓冲字符数组读取文件。public static void readTest2() throws IOException{//找到目标文件File file = new File("F:\\1208project\\day21\\src\\day21\\Demo1.java");// 建立数据的输入通道FileReader fileReader = new FileReader(file);//建立缓冲字符数组读取文件数据char[] buf = new char[1024];int length = 0 ; while((length = fileReader.read(buf))!=-1){System.out.print(new String(buf,0,length));}}public static void readTest1() throws IOException{//找到目标文件File file = new File("F:\\1208project\\day21\\src\\day21\\Demo1.java");//建立数据的输入通道FileReader fileReader = new FileReader(file);int content = 0 ;while((content = fileReader.read())!=-1){ //每次只会读取一个字符,效率低。System.out.print((char)content);}//关闭资源fileReader.close();}}

输出字符流:

------| Writer 输出字符流的基类。 抽象类
-----------| FileWriter 向文件输出数据的输出字符流

FileWriter的使用步骤:
1. 找到目标文件。
2. 建立数据输出通道
3. 写出数据。
4. 关闭资源

FileWriter要注意的事项:
1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它内部维护的字符数组中,如果需要
把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组。
2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。
3.使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先清空文件中的数据,然后再写入数据 , 如果需要在原来的基础上追加数据,
需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true。

public class Demo1 {public static void main(String[] args) throws IOException {writeTest1();}public static void  writeTest1() throws IOException{//找到目标文件File file = new File("F:\\a.txt");//建立数据输出通道FileWriter fileWriter = new FileWriter(file,true);//准备数据,把数据写出String data = "今天天气非常好!!";fileWriter.write(data);  //字符流具备解码的功能。//刷新字符流//fileWriter.flush();//关闭资源fileWriter.close();}}

练习: 使用字符流拷贝一个文本文件(java文件). 
接着使用字符流拷贝一个图片(观察图片的大小变化,思考为什么会这样子??)

//使用字符流拷贝文件public class Copy {public static void main(String[] args) throws IOException {BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\Test.txt"));BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\Test.exe"));String line=null;while((line = bufferedReader.readLine())!=null){bufferedWriter.write(line);}bufferedWriter.close();bufferedReader.close();}}

public class Copy {public static void main(String[] args) throws IOException {//copyText();copyImage();}public static void copyText() throws IOException{File inFile = new File("F:\\eee.txt");File outFile = new File("C:\\eee.txt");FileReader fr = new FileReader(inFile);FileWriter fw = new FileWriter(outFile);char[] buff = new char[1024];int length = 0;while((length = fr.read(buff))!=-1){fw.write(buff, 0, length);}fw.close();fr.close();}public static void copyImage() throws IOException{File inFile = new File("F:\\1.jpg");File outFile = new File("C:\\1.jpg");FileReader fr = new FileReader(inFile);FileWriter fw = new FileWriter(outFile);char[] buff = new char[1024];int length = 0;while((length = fr.read(buff))!=-1){fw.write(buff, 0, length);}fw.close();fr.close();}}



使用字符流拷贝图片会导致数据丢失:


何时使用字符流,何时使用字节流?依据是什么?
使用字符流的应用场景: 如果是读写字符数据的时候则使用字符流。
使用字节流的应用场景: 如果读写的数据都不需要转换成字符的时候,则使用字节流。 

0 0