java IO操做:FileInputStream,FileOutputStream,FileReader,FileWriter實例

FileInputStreamjava

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.InputStream;  
  4.   
  5. public class TestFileInputStream {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
  7.         // 第1步: 使用File類找到一個文件  
  8.         File f = new File("c:" + File.separator + "test.txt");// 聲明File 對象  
  9.         // 第2步: 經過子類實例化父類對象  
  10.         InputStream input = null;  
  11.         // 準備好一個輸入的對象, 經過對象多態性進行實例化  
  12.         input = new FileInputStream(f);  
  13.         // 第3步:進行讀操做, 全部的內容讀到此數組中  
  14.         byte b[] = new byte[1024];  
  15.         int len = input.read(b);  
  16.         // 第4步:關閉輸入流  
  17.         input.close();  
  18.         // 把byte數組變爲字符串輸出  
  19.         System.out.println("讀入數據的長度:" + len);  
  20.         System.out.println("內容爲:" + new String(b, 0, len));  
  21.     }  
  22. }</span>  

FileOutputStream數組

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.OutputStream;  
  4.   
  5. public class TestFileOutputStream {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出,不處理  
  7.         // 第1步: 使用File類找到一個文件  
  8.         File f = new File("c:" + File.separator + "test.txt"); // 聲明File對象  
  9.         // 第2步: 經過子類實例化父類對象  
  10.         OutputStream out = null;  
  11.         // 準備好一個輸出的對象, 經過對象多態性,進行實例化  
  12.         out = new FileOutputStream(f);  
  13.         // 第3步: 進行寫操做, 準備一個字符串  
  14.         String str = "Hello World!!!";  
  15.         // 只能輸出byte數組,因此將字符串變爲byte數組  
  16.         byte b[] = str.getBytes();  
  17.         // 將內容輸出,保存文件  
  18.         out.write(b);  
  19.         // 第4步:關閉輸出流  
  20.         out.close();  
  21.     }  
  22. }</span>  

FileReaderspa

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileReader;  
  3. import java.io.Reader;  
  4.   
  5. public class TestFileReader {  
  6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
  7.         // 第1步:使用File類找到一個文件, 聲明File對象  
  8.         File f = new File("d:" + File.separator + "test.txt");  
  9.         // 第2步:經過子類實例化父類對象  
  10.         Reader reader = null;  
  11.         // 準備好一個輸入的對象, 經過對象多態性進行實例化  
  12.         reader = new FileReader(f);  
  13.         // 第3步:進行讀操做, 全部的內容讀到此數組中  
  14.         char c[] = new char[1024];  
  15.         int len = reader.read(c);  
  16.         // 第4步:關閉輸入流  
  17.         reader.close();  
  18.         // 把char數組變爲字符串輸出  
  19.         System.out.println("內容爲:" + new String(c, 0, len));  
  20.     }  
  21. }</span>  

FileWriter對象

 
 
    1. <span style="font-family:Verdana;">import java.io.File;  
    2. import java.io.FileWriter;  
    3. import java.io.Writer;  
    4.   
    5. public class TestFileWriter {  
    6.     public static void main(String[] args) throws Exception { // 異常拋出, 不處理  
    7.         // 第1步:使用File類找到一個文件, 聲明File對象  
    8.         File f = new File("c:" + File.separator + "test.txt");  
    9.         // 第2步:經過子類實例化父類對象  
    10.         Writer out = null;  
    11.         // 準備好一個輸出的對象, 經過對象多態性, 進行實例化  
    12.         out = new FileWriter(f);  
    13.         // 第3步:進行寫操做, 準備一個字符串  
    14.         String str = "Hello World!!!";  
    15.         out.write(str);  
    16.         out.flush();  
    17.         // 第4步:關閉輸出流  
    18.         out.close();  
    19.     }  
    20. }</span>  
相關文章
相關標籤/搜索