根据处理数据的数据类型的不同分为:字节流与字符流
其中 字节流对应的两个抽象类:InputStream和OutputStream
字符流对应的两个抽象类:Reader和Writer
1: InputStream对应的子类为FileInputStream可实现读文件操作
2: OutputStream对应的子类为FileOutputStream可实现写文件操作
3: Reader对应的子类为FileReader可实现读文件操作
4: Writer对应的子类为FileWriter可实现写文件操作
一般来说,对于图片,音乐,视频采取的是字节流读写;对于文本文件或带有字符的文件则更多地采用字符流读写
InputStream与OutputStream(字节流):
package Test2016.demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo6 { public static void main(String[] args) { String src = "E:/demo6.txt"; String des = "E:/copy.txt"; FileInputStream fIn = null; FileOutputStream fOut = null; try { fIn = new FileInputStream(src); fOut = new FileOutputStream(des); byte[] buffer = new byte[1024]; int num = 0; //num的目的用于防止最后一次读取的字节小于buffer长度 while ((num = fIn.read(buffer)) != -1) { //参数1:写入的字符串;2:从开始写入字符的偏移量;3:要写入的字符数 System.out.println("共"+ num +"个字节"); fOut.write(buffer, 0, num); } } catch (IOException e) { System.out.println(e); } finally { try { if (null != fIn) fIn.close(); if (null != fOut) fOut.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Reader与Writer(字符流):
package Test2016.demo; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo7 { public static void main(String[] args) { String src = "E:/demo6.txt"; String des = "E:/copy.txt"; FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(src); fw = new FileWriter(des); char[] buffer = new char[1024]; int num = 0; while ((num = fr.read(buffer)) != -1) { System.out.println("共"+ num +"个字符"); fw.write(buffer, 0, num); } } catch (IOException e) { System.out.println(e); } finally { try { if (null != fr) fr.close(); if (null != fw) fw.close(); } catch (IOException e) { System.out.println(e); } } } }
对于大文件,需用缓冲区来读写文件,这样对不会给硬盘造成太大的负担
采用缓冲区可以有效的读写提高效率
理由:读取完放入缓存,然后一次性写入目的文件,不会因为边读边写不停地对目的文件进行写操作
Reader与Writer采用缓存区(一次读取一行):
package Test2016.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo7 { public static void main(String[] args) { String src = "E:/demo6.txt"; String des = "E:/copy.txt"; BufferedReader br = null; //创建缓冲区读对象引用 BufferedWriter bw = null; //创建缓冲区写对象引用 try { br = new BufferedReader(new FileReader(src)); bw = new BufferedWriter(new FileWriter(des)); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); bw.write(line); bw.newLine(); } bw.flush(); //将缓冲区的数据写到目的地,用缓存区必须执行此方法 } catch (IOException e) { System.out.println("读写时异常:" + e); } finally { try { if (null != br) br.close(); if (null != bw) bw.close(); } catch (IOException e) { System.out.println("关闭流时异常:" + e); } } } }
Reader与Writer采用缓存区(一次读取指定长度字符):
package Test2016.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; public class Demo7 { public static void main(String[] args) { String src = "E:/demo6.txt"; String des = "E:/copy.txt"; FileReader fr = null; FileWriter fw = null; BufferedReader br = null; BufferedWriter bw = null; try { fr = new FileReader(src); fw = new FileWriter(des); br = new BufferedReader(fr); bw = new BufferedWriter(fw); char[] buffer = new char[512]; int num = 0; while ((num = br.read(buffer)) != -1) { bw.write(buffer, 0, num); } bw.flush(); } catch (IOException e) { System.out.println("读写时异常:" + e); } finally { try { if (null != br) fr.close(); if (null != bw) fw.close(); } catch (IOException e) { System.out.println("关闭流时异常:" + e); } } } }
InputStream与OutputStream采用缓冲区:
package Test2016.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo6 { public static void main(String[] args) { String src = "E:/demo6.txt"; String des = "E:/copy.txt"; // FileInputStream fIn = null; // FileOutputStream fOut = null; BufferedInputStream bIs = null; BufferedOutputStream bOs = null; try { /** fIn = new FileInputStream(src); fOut = new FileOutputStream(des); bIs = new BufferedInputStream(fIn); bOs = new BufferedOutputStream(fOut); */ bIs = new BufferedInputStream(new FileInputStream(src)); bOs = new BufferedOutputStream(new FileOutputStream(des)); byte[] buffer = new byte[1024]; int num = 0; while ((num = bIs.read(buffer)) != -1) { //用到num不用buffer.length的主要原因: //是由于最后一次循环如果不够buffer数组定义的长度的话,不够的部分会被默认填充0,而用num就不会 bOs.write(buffer, 0, num); } bOs.flush(); //将缓存区的内容写进目的文件 } catch (IOException e) { System.out.println("读写文件时异常:" + e); } finally { try { if (null != bIs) bIs.close(); if (null != bOs) bOs.close(); } catch (IOException e) { System.out.println("读写文件时异常:" + e); } } } }