IO流简介
文件流(节点流)
字节流:FileInputStream,FileOutputStream
字符流:FileReader,FileWriter
练习:用字节流和字符流分别完成文件的复制
1)字节流(完成图片的copy) :非文本(视频,图片)
1 public void copy(){ 2 //1.jpg的绝对路径:D:\MyEclipse\MyEclipseWorkplace\FileTest\1.jpg
File file = new File("1.jpg");
File file2 = new File("2.jpg"); 5 FileInputStream fis = null; 6 FileOutputStream fos = null; 7 try { 8 fis = new FileInputStream(file); 9 fos = new FileOutputStream(file2); 10 byte[] b = new byte[5]; 11 int len; 12 while((len=fis.read(b))!=-1){ 13 // fos.write(b);==fos.write(b,0,b.length); 错误的写法 图片大小会变大 14 fos.write(b, 0, len); //每次将缓冲字节数组的len个字节写到输入流中 15 } 16 } catch (Exception e) { 17 e.printStackTrace(); 18 }finally{ 19 //关闭流20 }
}
2)字符流(文本之间的copy)
1 //需求:用FileReader+FileWriter实现拷贝功能 2 @Test 3 public void copy(){ 4 File file = new File("dbcp.txt"); //文件必须存在 5 File file2 = new File("dbcp2.txt"); //文件可以不存在,存在会覆盖掉,不存在会自己创建文件 6 FileReader fr = null; 7 FileWriter fw = null; 8 try { 9 fr = new FileReader(file); 10 fw = new FileWriter(file2); 11 char[] cha = new char[20]; 12 int len; 13 while((len=fr.read(cha))!=-1){ 14 fw.write(cha, 0, len); 15 } 16 } catch (Exception e) { 17 e.printStackTrace(); 18 }finally{ //关闭流32 } 33 }
缓冲流(处理流) 开发时常用。 节点流的外套,提高传输效率
BuffferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter
练习:用缓冲字节流和缓冲字符流分别完成文件的复制
1)字节缓冲流
1 //使用缓冲流实现文件的复制的方法 2 public void copyFile(){ 3 BufferedInputStream bis = null; 4 BufferedOutputStream bos = null; 5 try { 6 //1.提供读入、写出的文件 7 File file1 = new File("C:\\Users\\shkstart\\Desktop\\1.avi"); 8 File file2 = new File("C:\\Users\\shkstart\\Desktop\\2.avi"); 9 //2.想创建相应的节点流:FileInputStream、FileOutputStream 10 FileInputStream fis = new FileInputStream(file1); 11 FileOutputStream fos = new FileOutputStream(file2); 12 //3.将创建的节点流的对象作为形参传递给缓冲流的构造器中 13 bis = new BufferedInputStream(fis); 14 bos = new BufferedOutputStream(fos); 15 //4.具体的实现文件复制的操作 16 byte[] b = new byte[1024]; 17 int len; 18 while((len = bis.read(b)) != -1){ 19 bos.write(b, 0, len); 20 bos.flush(); 21 } 22 }catch (IOException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 }finally{ 26 //5.关闭相应的流 46 } 47 }
2)缓冲字符流
1 @Test 2 public void testBufferedReader(){ 3 BufferedReader br = null; 4 BufferedWriter bw = null; 5 try { 6 File file = new File("dbcp.txt"); 7 File file1 = new File("dbcp3.txt"); 8 FileReader fr = new FileReader(file); 9 10 FileWriter fw = new FileWriter(file1); 11 br = new BufferedReader(fr); 12 bw = new BufferedWriter(fw); 13 // char[] c = new char[1024]; 14 // int len; 15 // while((len = br.read(c))!= -1){ 16 // String str = new String(c, 0, len); 17 // System.out.print(str); 18 // } 19 20 String str; 21 while((str = br.readLine()) != null){ //直接读取关联文件中的一行 22 // System.out.println(str); 23 bw.write(str + "\n"); //直接写入一行 24 // bw.newLine(); 25 bw.flush(); 26 } 27 }catch (IOException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 }finally{ //关闭流 50 } 51 } 52
转换流
转换流提供了在字节流和字符流之间的转换
转换流:InputStreamReader OutputStreamWriter
InputStreamReader
用于将字节流中读取到的字节按指定字符集解码成字符。需要和InputStream“套接”。
1 br = new BufferedReader(new FileReader("test.txt")); 2 bw = new BufferedWriter(new FileWriter("test3.txt")); 3 String str ; 4 while((str=br.readLine())!=null){ 5 bw.write(str); 6 bw.newLine(); 7 bw.flush(); 8 }
System.in和System.out分别代表了系统标准的输入和输出设备
默认输入设备是键盘,输出设备是显示器控制台