字符流(cha[]数组)html
实例化File类的对象,指明要操做的文件java
注意:数组
在@Test测试中的相对路径是相对于当前Module函数
在main函数的相对路径是相对于当前工程测试
提供具体的流指针
数据的读入过程code
流的关闭操做视频
try-catch-finally异常处理htm
@org.junit.Test public void test1(){ FileReader fileReader = null; try { //1. 实例化File类的对象,指明要操做的文件 //注意:在@Test测试中的相对路径是相对于当前Module,在main函数的相对路径是相对于当前工程 File file = new File("hello.txt");//相对路径 //2. 提供具体的流 fileReader = new FileReader(file); //3. 数据的读入过程 //read():返回读入的一个字符。若是达到文件末尾,返回-1 //方式一 // int data = fileReader.read(); // while(data != -1){ // System.out.print((char)data); // data = fileReader.read(); // } //方式二:语法上针对方式一的修改 int data; while((data = fileReader.read()) != -1){ System.out.println((char)data); } } catch (IOException e) { e.printStackTrace(); }finally { //4. 流的关闭操做 try { //防止最开始提供流是出现异常致使这里是空指针,因此加一个判断 if(fileReader != null){ fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } }
此时的"hello.txt"文档中存储的内容是hello对象
建立一个char的数组cbuf
(举例假定该数组的长度为5)
char[] cbuf = new char[5];
建立len变量,存储read(char[] cbuf)的返回值
(注意:返回值即为每次读入cbuf数组中的字符的个数)
读取数据
for (int i = 0; i < len; i++) { System.out.print(cbuf[i]); }
String str = new String(cbuf,0,len); System.out.print(str);
http://www.javashuo.com/article/p-pzykigqu-mx.html
流的关闭操做(不要忘记)
写法一
for (int i = 0; i < len; i++) { System.out.print(cbuf[i]); }
其中for循环时,i必定要小于len(每次读入cbuf数组中的字符的个数)而不是cbuf.length
画图说明:
最后还剩下3个数据的时候:
若是for循环中的i小于cbuf.length(错误的写法)
最后的输出为:helloWorld123ld
(其中hello为第一次的结果,World为第二次的结果,123ld为第三次的结果)
而文件中的内容为helloWorld123,因此此时的写法是错误的
若是for循环中的i小于len(正确的写法)
最后的输出为:helloWorld123
(其中hello为第一次的结果,World为第二次的结果,123为第三次的结果)
写法二
String str = new String(cbuf,0,len); System.out.print(str);
其中利用了String(char[] value, int offset, int count)构造器,而不是String(char[] value)构造器
String(char[] value)构造器(错误的写法)
错误缘由与写法一错误的方法相同
String(char[] value, int offset, int count)构造器(正确的写法)
读取char[]数组中角标0到角标len的内容,过程与写法一正确的方法过程相似
写出操做,对应的File能够不存在。
对原有文件的覆盖
流使用的构造器为
FileWriter(file, false)
或者是
FileWriter(file)
在原有文件的基础上追加内容(不会对原有文件进行覆盖)
流使用的构造器为
FileWriter(file, true)
FileWriter fw1 = null; FileWriter fw2 = null; try { //1. 提供File类的对象,指明写出到的文件 File file = new File("hi.txt"); //2. 提供FileWriter的对象,用于文件的写出 //覆盖 fw1 = new FileWriter(file,false); //不覆盖 fw2 = new FileWriter(file,true); //3. 写出的操做 fw1.write("I have a dream!\n"); fw2.write("123\n"); } catch (IOException e) { e.printStackTrace(); }finally { //4. 关闭流资源 if(fw1 != null) try { fw1.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(fw2 != null) try { fw2.close(); } catch (IOException e) { e.printStackTrace(); } } }
最开始hi.txt文件中的内容为hello
运行代码后
在读取文件文件时,选择read(char[] cbuf)的用法
在写入文件的时候,注意不要写成
fw.write(cbuf);
此时出现的错误和读取操做时写法一会出现的错误相同,不能够正确的读取文件中本来的内容
须要使用
fw.write(cbuf,0,len);
读取cbuf数组中角标为0的元素到角标为len的元素
在最后关闭流资源的时候,必定要注意两个流都要关闭,即便其中一个流在关闭的时候抛出异常另外一个流也要关闭
有两种写法
finally { //4. 关闭流资源 if(fr != null) try { fr.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(fw != null) try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
finally { //4. 关闭流资源 try { if(fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fw != null) fw.close(); } catch (IOException e) { e.printStackTrace(); } }
public void test4(){ FileReader fr = null; FileWriter fw = null; try { //1. 建立File类的对象,指明读入和写出的文件 File file1 = new File("hello.txt"); File file2 = new File("hi.txt"); //2. 建立输入流和输出流的对象 fr = new FileReader(file1); fw = new FileWriter(file2,true); //3. 数据的读入和写入操做 char[] cbuf = new char[5]; int len;//记录每次读入到cbuf数组中字符的个数 while((len = fr.read(cbuf)) != -1){ //每次写出len个字符 fw.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); }finally { //4. 关闭流资源 try { if(fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fw != null) fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
字符流不能处理图片文件等字节数据!!!
字节流(byte[])
FileInputStream fis = null; FileOutputStream fos = null; try { File srcfile = new File("殷志源.png"); File destfile = new File("丸子.png"); fis = new FileInputStream(srcfile); fos = new FileOutputStream(destfile); byte[] buffer = new byte[5]; int len; while((len = fis.read(buffer)) != -1){ fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } }
复制视频
public void copyFile(String srcPath, String destPath){ FileInputStream fis = null; FileOutputStream fos = null; try { File srcfile = new File(srcPath); File destfile = new File(destPath); fis = new FileInputStream(srcfile); fos = new FileOutputStream(destfile); byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } }