java中的IO操作總結(二)

 字節流

 
實例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,視頻文件等
說白了在硬盤上都是以字節存儲的,只不過字符流在操作文本上面更方便一點而已
此外,爲什麼要利用緩衝區呢?
我們知道,像迅雷等下載軟件都有個緩存的功能,硬盤本身也有緩衝區
試想一下,如果一有數據,不論大小就開始讀寫,勢必會給硬盤造成很大負擔,它會感覺很不爽
人不也一樣,一頓飯不讓你一次吃完,每分鐘喂一勺,你怎麼想?
因此,採用緩衝區能夠在讀寫大文件的時候有效提高效率

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章