java io 筆記二:FileOutPutStream、FileInPutStream、FileWriter、FileReader類

FileOutPutStream 和FileInPutStream:java

  
  
  
  
  1. package iotest;   
  2.    
  3.    
  4.    
  5. import java.io.*;   
  6.    
  7. public class StreamTest {   
  8.    
  9.     /**  
  10.      * 流的概念:  
  11.      *  一、流是字節順序的抽象概念。  
  12.      *  二、文件是數據的靜態存儲形式、而流事是指數據傳輸時的形態。  
  13.      *  三、流類分爲兩個大類:節點流和過濾流類(也叫處理流類)。  
  14.   
  15.      *  InputStream類:  
  16.      *  程序能夠從中連續讀取字節的對象叫輸入流,在java中,用InputStream類來描述全部輸入流的抽象概念。  
  17.   
  18.      *  OutPutStream類:  
  19.      *  程序能夠向其中連續寫入字節的對象叫輸出流,在java中,用OutPutStream類來描述全部輸出流的抽象概念。  
  20.   
  21.      *  FuleInputStream和FileOutPutStream類分別用來建立磁盤文件的輸入流個輸出流對象、經過他們的構造函數來指點文件路徑文件名。  
  22.   
  23.      *  建立FuleInputStream實例對象時,指定的文件應該是存在和可讀的,建立FileOutPutStream實例對象時,若是指定的文件已經存在,這個文件中的原來內容將被覆蓋清楚。  
  24.   
  25.      *  對同一個磁盤文件建立FuleIntputStream對象的兩種方式:  
  26.      *  (1)、FileInPutStream inOne = new FileInPutStream("hello.test");  
  27.      *  (2)、File f = new ("hello.test");  
  28.      *       FileInPutStream inTwo = new FileInPutStream(f);  
  29.        
  30.      *  建立FileOutPutStream實例對象時,能夠指定還不存在的文件名,不能指定一個已被其餘程序打開了的文件。  
  31.       
  32.       
  33.      * 例題:用FileOutPutStream類向文件中寫入一個字符串,而後用FileInPutStream讀出寫入的內容  
  34.      * @param args  
  35.      * @throws IOException   
  36.      */   
  37.     public static void main(String[] args) throws IOException {   
  38.            
  39.         /* FileOutputStream類:文件輸出流是用於將數據寫入 File 或 FileDescriptor 的輸出流。  
  40.          *   
  41.          *   
  42.          * FileOutputStream(String name)構造:建立一個向具備指定名稱的文件中寫入數據的輸出文件流。  
  43.          *   
  44.          */   
  45.         FileOutputStream fout = new FileOutputStream("hzw//hello.txt");   
  46.            
  47.            
  48.         /*  
  49.          * Writer方法: b.length 個字節從指定 byte 數組寫入此文件輸出流中。  
  50.          *   
  51.          * String.getBytes()方法用於將字符串轉換爲字節數組  
  52.          */   
  53.         fout.write("www.hzw.com".getBytes());   
  54.            
  55.            
  56.         //關閉流   
  57.         fout.close();   
  58.            
  59.            
  60.         /*  
  61.          * 建立一個file對象: 文件和目錄路徑名的抽象表示形式。  
  62.          *   
  63.          * File(String name)構造:經過將給定路徑名字符串轉換爲抽象路徑名來建立一個新 File 實例。  
  64.          */   
  65.         File f = new File("hzw//hello.txt");   
  66.            
  67.         /*  
  68.          * FileInputStream類:用於讀取諸如圖像數據之類的原始字節流。要讀取字符流,請考慮使用 FileReader。  
  69.          *   
  70.          * FileInputStream(File file)構造:經過打開一個到實際文件的鏈接來建立一個 FileInputStream,  
  71.          *                                 該文件經過文件系統中的 File 對象 file 指定。  
  72.          */   
  73.         FileInputStream input = new FileInputStream(f);   
  74.            
  75.         /*  
  76.          * 建立一個byte(字節)類型數組  
  77.          */   
  78.         byte[] by = new byte[1024];   
  79.            
  80.         /*  
  81.          * read方法:今後輸入流中讀取一個數據字節。  
  82.          *   
  83.          * read(byte[] b)方法:今後輸入流中將最多 b.length 個字節的數據讀入一個 byte 數組中。  
  84.          *   
  85.          * 返回:讀入緩衝區的字節總數,若是由於已經到達文件末尾而沒有更多的數據,則返回 -1。  
  86.          */   
  87.         int len = input.read(by);   
  88.            
  89.            
  90.         /*  
  91.          * String(by,0,len)構造: 用於將by數組轉換成字符串——從0開始到len(Int變量值)結束  
  92.          */   
  93.         System.out.println(new String(by,0,len));   
  94.            
  95.         //關閉流   
  96.         input.close();       
  97.            
  98.     }   
  99.    
  100. }  

FileWriter、FileReader類:數組

  
  
  
  
  1. package iotest;   
  2.    
  3. import java.io.*;   
  4. public class IoStringTest {   
  5.    
  6.     /**  
  7.      * java 中專門有兩個類分別對字符串進行輸入跟輸出處理。Reader、Writer類  
  8.      *   
  9.      * 使用FileWriter類向文件中寫入一個字符串,而後用FileReader類讀出寫入的內容  
  10.      *   
  11.      * @param args  
  12.      * @throws IOException   
  13.      */   
  14.     public static void main(String[] args) throws IOException {   
  15.         /*  
  16.          * FileWriter(繼承OutputStreamWriter類 ):用來寫入字符文件的便捷類。  
  17.          *   
  18.          * FileWriter(String name):根據給定的文件名構造一個 FileWriter 對象。  
  19.          */   
  20.         FileWriter wr = new FileWriter("hzw//tiantian.txt");   
  21.            
  22.         /*  
  23.          * write:繼承OutputStreamWriter類 write方法——的寫入字符串。  
  24.          */   
  25.         wr.write("何祖文");   
  26.            
  27.         //有興趣的能夠把這句代碼刪掉試試!!!   
  28.         wr.close();   
  29.            
  30.         char[] ch = new char[1024];   
  31.            
  32.         /*  
  33.          * FileReader(繼承InputStreamReader類):用來讀取字符文件的便捷類。  
  34.          *   
  35.          * FileReader(String fileName):在給定從中讀取數據的文件名的狀況下建立一個新 FileReader。  
  36.          */   
  37.         FileReader re = new FileReader("hzw//tiantian.txt");   
  38.            
  39.         /*  
  40.          * read:繼承InputStreamReader類的read方法——試圖將字符讀入指定的字符緩衝區。  
  41.          *   
  42.          * 返回:添加到緩衝區的字符數量,若是此字符源位於緩衝區末端,則返回 -1   
  43.          */   
  44.         int len = re.read(ch);   
  45.         System.out.println(new String(ch,0,len));   
  46.         re.close();   
  47.     }   
  48.    
相關文章
相關標籤/搜索