java中的IO操做总结(二)

 字节流java

 
实例5:字节流的写入
  
  
  
  
  1. import java.io.File; 
  2. import java.io.FileOutputStream; 
  3. import java.io.IOException; 
  4.   
  5. public class Demo { 
  6.     public static void main(String[] args ) { 
  7.            
  8.         String path = File.separator + "home" + File.separator + "siu" + 
  9.                       File.separator + "work" + File.separator + "demo.txt"
  10.           
  11.         FileOutputStream o = null
  12.           
  13.         try { 
  14.             o = new FileOutputStream(path); 
  15.             String str = "Nerxious is a good boy\r\n"
  16.             byte[] buf = str.getBytes(); 
  17.             //也能够直接使用o.write("String".getBytes()); 
  18.             //由于字符串就是一个对象,能直接调用方法 
  19.             o.write(buf); 
  20.               
  21.         } catch (IOException e) { 
  22.             e.printStackTrace(); 
  23.         } finally { 
  24.             if(o != null) { 
  25.                 try { 
  26.                     o.close(); 
  27.                 } catch (IOException e) { 
  28.                     e.printStackTrace(); 
  29.                 } 
  30.             } 
  31.         } 
  32.      
  33.     } 
 
 
 
编译以后产生的文件,以上在字符串中加\r\n就是为了便于终端显示
其实在linux下面换行仅用\n便可
 
 
实例6:字节流的读取
  
  
  
  
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.IOException; 
  4.   
  5. public class Demo { 
  6.     public static void main(String[] args ) { 
  7.            
  8.         String path = File.separator + "home" + File.separator + "siu" + 
  9.                       File.separator + "work" + File.separator + "demo.txt"
  10.           
  11.         FileInputStream i = null
  12.           
  13.         try { 
  14.             i = new FileInputStream(path); 
  15.               
  16.             //方式一:单个字符读取 
  17.             //须要注意的是,此处我用英文文本测试效果良好 
  18.             //但中文就悲剧了,不过下面两个方法效果良好 
  19.             int ch = 0
  20.             while((ch=i.read()) != -1){ 
  21.                 System.out.print((char)ch); 
  22.             } 
  23.               
  24.             //方式二:数组循环读取 
  25.             /* 
  26.             byte[] buf = new byte[1024]; 
  27.             int len = 0; 
  28.             while((len = i.read(buf)) != -1) { 
  29.                 System.out.println(new String(buf,0,len)); 
  30.             } 
  31.             */ 
  32.               
  33.               
  34.             //方式三:标准大小的数组读取 
  35.             /* 
  36.             //定一个一个恰好大小的数组 
  37.             //available()方法返回文件的字节数 
  38.             //可是,若是文件过大,内存溢出,那就悲剧了 
  39.             //因此,亲们要慎用!!!上面那个方法就不错 
  40.             byte[] buf = new byte[i.available()]; 
  41.             i.read(buf); 
  42.             //由于数组大小恰好,因此转换为字符串时无需在构造函数中设置起始点 
  43.             System.out.println(new String(buf)); 
  44.             */ 
  45.               
  46.         } catch (IOException e) { 
  47.             e.printStackTrace(); 
  48.         } finally { 
  49.             if(i != null) { 
  50.                 try { 
  51.                     i.close(); 
  52.                 } catch (IOException e) { 
  53.                     e.printStackTrace(); 
  54.                 } 
  55.             } 
  56.         } 
  57.      
  58.     } 
读取文件到终端

 


实例7:二进制文件的复制
  
  
  
  
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream; 
  4. import java.io.IOException; 
  5.   
  6. public class Demo { 
  7.     public static void main(String[] args ) { 
  8.            
  9.         String bin = File.separator + "home" + File.separator + "siu" + 
  10.                       File.separator + "work" + File.separator + "一我的生活.mp3"
  11.           
  12.         String copy = File.separator + "home" + File.separator + "siu" + 
  13.                       File.separator + "life" + File.separator + "一我的生活.mp3"
  14.           
  15.         FileInputStream i = null
  16.         FileOutputStream o = null
  17.           
  18.         try { 
  19.             i = new FileInputStream(bin); 
  20.             o = new FileOutputStream(copy); 
  21.               
  22.             //循环的方式读入写出文件,从而完成复制 
  23.             byte[] buf = new byte[1024]; 
  24.             int temp = 0
  25.             while((temp = i.read(buf)) != -1) { 
  26.                 o.write(buf, 0, temp); 
  27.             } 
  28.   
  29.         } catch (IOException e) { 
  30.             e.printStackTrace(); 
  31.         } finally { 
  32.             if(i != null) { 
  33.                 try { 
  34.                     i.close(); 
  35.                 } catch (IOException e) { 
  36.                     e.printStackTrace(); 
  37.                 } 
  38.             } 
  39.             if(o != null) { 
  40.                 try { 
  41.                     o.close(); 
  42.                 } catch (IOException e) { 
  43.                     e.printStackTrace(); 
  44.                 } 
  45.             } 
  46.         } 
  47.     } 

 

复制效果,如图:

 

实例8:利用字节流的缓冲区进行二进制文件的复制
  
  
  
  
  1. import java.io.BufferedInputStream; 
  2. import java.io.BufferedOutputStream; 
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileOutputStream; 
  6. import java.io.IOException; 
  7.   
  8. public class Demo { 
  9.     public static void main(String[] args ) { 
  10.            
  11.         String bin = File.separator + "home" + File.separator + "siu" + 
  12.                       File.separator + "work" + File.separator + "一我的生活.mp3"
  13.           
  14.         String copy = File.separator + "home" + File.separator + "siu" + 
  15.                       File.separator + "life" + File.separator + "一我的生活.mp3"
  16.           
  17.         FileInputStream i = null
  18.         FileOutputStream o = null
  19.         BufferedInputStream bi = null
  20.         BufferedOutputStream bo = null
  21.           
  22.         try { 
  23.             i = new FileInputStream(bin); 
  24.             o = new FileOutputStream(copy); 
  25.             bi = new BufferedInputStream(i); 
  26.             bo = new BufferedOutputStream(o); 
  27.               
  28.             byte[] buf = new byte[1024]; 
  29.             int temp = 0
  30.             while((temp = bi.read(buf)) != -1) { 
  31.                 bo.write(buf,0,temp); 
  32.             } 
  33.               
  34.         } catch (IOException e) { 
  35.             e.printStackTrace(); 
  36.         } finally { 
  37.             if(bi != null) { 
  38.                 try { 
  39.                     i.close(); 
  40.                 } catch (IOException e) { 
  41.                     e.printStackTrace(); 
  42.                 } 
  43.             } 
  44.             if(bo != null) { 
  45.                 try { 
  46.                     o.close(); 
  47.                 } catch (IOException e) { 
  48.                     e.printStackTrace(); 
  49.                 } 
  50.             } 
  51.         } 
  52.     } 

 

两个目录都有 “一我的生活.mp3”文件,顺便说一下,这歌挺好听的

 

初学者在学会使用字符流和字节流以后未免会产生疑问:何时该使用字符流,何时又该使用字节流呢?
其实仔细想一想就应该知道,所谓字符流,确定是用于操做相似文本文件或者带有字符文件的场合比较多
而字节流则是操做那些没法直接获取文本信息的二进制文件,好比图片,mp3,视频文件等
说白了在硬盘上都是以字节存储的,只不过字符流在操做文本上面更方便一点而已
此外,为何要利用缓冲区呢?
咱们知道,像迅雷等下载软件都有个缓存的功能,硬盘自己也有缓冲区
试想一下,若是一有数据,不论大小就开始读写,势必会给硬盘形成很大负担,它会感受很不爽
人不也同样,一顿饭不让你一次吃完,每分钟喂一勺,你怎么想?
所以,采用缓冲区可以在读写大文件的时候有效提升效率
相关文章
相关标签/搜索