Java字節流:FileInputStream FileOutputStream


-----------------------------------------------------------------------------------

FileInputStream
類聲明:public class FileInputStream extends InputStream
位於java.io包下
官方對其說明:
  A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment..
  (簡單翻譯:FileInputStream從文件系統中的某個文件中獲得輸入字節.)

構造方法

  FileInputStream(File file);

    通過打開一個到實際文件的連接來創建一個FileInputStream實例,該文件通過文件系統中的File對象file指定。

  FileInputStream(String name);
    通過打開一個到實際文件的連接來創建一個FileInputStream實例,該文件通過文件系統中的路徑名name指定。

  FileInputStream(FileDescriptor fdObj);
    通過使用文件描述符fdObj創建一個FileInputStream實例。

主要方法:

  - int available(): 返回字節文件輸入流中可讀取的字節數
  - void close(): 關閉此文件輸入流並釋放與該流有關的系統資源.
  - protected void finalize(): 確保在不再引用文件輸入流時調用其close()方法.
  - int read(): 從文件輸入流中讀取一個字節數據
  - int read(byte[] b): 從文件輸入流中將最多b.length個字節數據讀入到字節數組b中
  - int read(byte[] b,int off,int len): 從文件輸入流中將最多len個字節的數據讀入到字節數組b中
  - long skip(long n): 從文件輸入流中跳過並丟棄n個字節的數據

查看源代碼
  FileInputStream這個類的作用就是把文件中的內容讀取到程序中去,其中最關鍵的就是三個read方法,其源碼都是通過調用native方法來實現的。

 1 public native int read() throws IOException;
 2 public int read(byte b[]) throws IOException {
 3         return readBytes(b, 0, b.length);
 4     }
 5 public int read(byte b[], int off, int len) throws IOException {
 6         return readBytes(b, off, len);
 7     }
 8 private native int readBytes(byte b[], int off, int len) throws IOException;
 9 public native long skip(long n) throws IOException;
10 public native int available() throws IOException;
View Code

 

------------------------------------------------------------------------------------------------------------------

FileOutputStream
類聲明:public class FileOutputStream extends OutputStream
位於java.io包下
官方對其說明:
  A file output stream is an output stream for writing data to a File or to a FileDescriptor.
  (簡單翻譯:FileOutputStream文件輸出流失用於將數據寫入File或者FileDescriptor的輸出流.)

構造方法:
  FileOutputStream(File file);
    創建一個向指定File對象表示的文件中寫入數據的文件輸出流。

  FileOutputStream(File file,boolean append);
    創建一個向指定File對象表示的文件中寫入數據的文件輸出流。

  FileOutputStream(String name);
    創建一個向具有指定名稱的文件中寫入數據的輸出文件流。

  FileOutputStream(String name,boolean append);
    創建一個向具有指定name的文件中寫入數據的輸出文件流。

  FileOutputStream(FileDescriptor fdObj);
    通過使用文件描述符fdObj創建一個FileOutputStream實例。

主要方法:
  - void close(): 關閉此文件輸出流並釋放與該流有關的系統資源.
  - protected void finalize(): 清理文件的鏈接,確保在不再引用文件輸出流時調用其close()方法.
  - void write(byte[] b): 將b.length個字節從指定的字節數組b寫入此文件輸出流。
  - void write(byte[] b,int off,int len): 將指定的字節數組b從偏移量off開始的len個字節寫入此文件輸出流。
  - void write(int b): 將指定的字節寫入此文件輸出流

查看源代碼:
  FileOutputStream這個類的作用就是把程序中的字節數據寫入到指定的文件中,其中最關鍵的就是三個write方法,其源碼都是通過調用native方法來實現的。

 1 private native void write(int b, boolean append) throws IOException;
 2 public void write(int b) throws IOException {
 3         write(b, append);
 4     }
 5 private native void writeBytes(byte b[], int off, int len, boolean append) throws IOException;
 6 public void write(byte b[]) throws IOException {
 7         writeBytes(b, 0, b.length, append);
 8     }
 9 public void write(byte b[], int off, int len) throws IOException {
10         writeBytes(b, off, len, append);
11     }
View Code

FileInputStream和FileOutputStream舉例: 實現文件的復制.

fileIn.txt文件中的內容:
  abcdefgheretrtrt

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 public class FileInputOutputStreamDemo {
 7     
 8     public static void main(String[] args) throws IOException {
 9         //目標文件
10         File file = new File("fileIn.txt");
11         //創建FileInputStream實例,該文件通過文件系統中的File對象file指定
12         FileInputStream fis = new FileInputStream(file);
13         
14         byte[] fileInput = new byte[1024];
15         int count = 0;
16         int bytes = fis.read();
17         //循環將文件fileText.txt中的內容讀取到字節數組fileInput中
18         while(bytes != -1){
19             fileInput[count++] = (byte)bytes;
20             bytes = fis.read();
21         }
22         
23         //輸出文件
24         File fileOutput = new File("fileOut.txt");
25         if(!fileOutput.exists()){
26             fileOutput.createNewFile();
27         }
28         //創建文件輸出流對象
29         FileOutputStream fos = new FileOutputStream(fileOutput);
30         //將字節數組fileInput中的內容輸出到文件fileOut.txt中
31         fos.write(fileInput);
32     }
33 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM