1. 程式人生 > >Java中的FileInputStream與FileOutputStream的基本使用詳解

Java中的FileInputStream與FileOutputStream的基本使用詳解

什麼是InputStream和OutputStream?

InputStream和OutputStream是抽象類,是所有位元組輸入流和輸出流的父類。這裡,我們首先要分清楚兩個概念:

  • InputStream(輸入流):輸入流是用來讀入資料的。- - - > > >讀入
  • OutputStream(輸出流):輸出流是用來寫出資料的。- - - > > >寫出

檔案輸入流——FileInputStream

FileInputStream 從檔案系統中的某個檔案中獲得輸入位元組。

構造方法

//通過開啟一個到實際檔案的連線來建立一個 FileInputStream,該檔案通過檔案系統中的 File 物件 file 指定。
public FileInputStream(File file); //通過開啟一個到實際檔案的連線來建立一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定。 public FileInputStream(String name);

常用的方法

從輸入流中讀取一個位元組大小的資料

//從此輸入流中讀取一個數據位元組。
public int read();

從輸入流一次讀取一個位元組陣列

//從此輸入流中將最多 b.length 個位元組的資料讀入一個 byte 陣列中。
public int read(byte[] b);

//從此輸入流中將最多 len 個位元組的資料讀入一個 byte 陣列中。off:目標陣列 b 中的起始偏移量。
public int read(byte[] b,int off,int len);

從檔案中讀取資料:

import java.io.FileInputStream;

/**
 * FileInputStream:節點流(低階流),從檔案中讀入資料
 * @author Administrator
 *
 */
public class FISDemo01 {
    public static void main(String[] args){
        String content=null;
        try {
            int size=0;
            //定義一個位元組緩衝區,該緩衝區的大小根據需要來定義
byte[] buffer=new byte[1024]; FileInputStream fis=new FileInputStream("FOSDemo.txt"); //迴圈來讀取該檔案中的資料 while((size=fis.read(buffer))!=-1){ content=new String(buffer, 0, size); System.out.println(content); } //關閉此檔案輸入流並釋放與此流有關的所有系統資源。 fis.close(); } catch (Exception e) { e.printStackTrace(); } } }

檔案輸出流——FileOutputStream

檔案輸出流是用於將資料寫入到檔案中。

構造方法

//建立一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流。
public FileOutputStream(File file);

//建立一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流。如果第二個引數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。
public FileOutputStream(File file,boolean append);

//建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流。
public FileOutputStream(String name);

//建立一個向具有指定 name 的檔案中寫入資料的輸出檔案流。如果第二個引數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。
public FileOutputStream(String name,boolean append);

常用的方法

向檔案中寫入一個位元組大小的資料

//向檔案中寫入一個位元組大小的資料
public void write(int b);

向檔案中一次性寫入一個位元組陣列的資料

//將 b.length 個位元組從指定 byte 陣列寫入此檔案輸出流中。
public void write(byte[] b);

//指定 byte 陣列中從偏移量 off 開始的 len 個位元組寫入此檔案輸出流。 
public void write(byte[] b,int off,int len);

向檔案中寫出資料:

import java.io.FileOutputStream;

/**
 * FileOutputStream:節點流(低階流),向檔案中寫出資料 
 * @author Administrator
 *
 */
public class FOSDemo01 {
    public static void main(String[] args){
        try {
            //向檔案中寫入位元組陣列
            String font="輸出流是用來寫入資料的!";
            FileOutputStream fos = new FileOutputStream("FOSDemo.txt");
            fos.write(font.getBytes());
            //關閉此檔案輸出流並釋放與此流有關的所有系統資源。此檔案輸出流不能再用於寫入位元組。 如果此流有一個與之關聯的通道,則關閉該通道。 
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用FileInputStream和FileOutputStream實現檔案的複製

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 使用檔案輸入流和檔案輸出流實現檔案的複製
 * @author Administrator
 *
 */
public class SummaryFISAndFOS {
    public static void main(String[] args){
        /**
         * 1.先將檔案中的內容讀入到輸入流中
         * 2.將輸入流中的資料通過輸出流寫入到目標檔案中
         * 3.關閉輸入流和輸出流
         */
        try {
            long begin=System.currentTimeMillis();
            //從輸入流中讀取資料
            FileInputStream fis=new FileInputStream("FOSDemo.txt");
            //向輸出流中寫入資料
            FileOutputStream fos=new FileOutputStream("FISAndFOSDest.txt");
            //先定義一個位元組緩衝區,減少I/O次數,提高讀寫效率
            byte[] buffer=new byte[10240];
            int size=0;
            while((size=fis.read(buffer))!=-1){
                fos.write(buffer, 0, size);
            }
            fis.close();
            fos.close();
            long end=System.currentTimeMillis();
            System.out.println("使用檔案輸入流和檔案輸出流實現檔案的複製完畢!耗時:"+(end-begin)+"毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //解決JNI問題(Java Native Interface)
        System.exit(0);
    }
}

執行結果:
使用檔案輸入流和檔案輸出流實現檔案的複製完畢!耗時:17毫秒

以上基本上就是我們經常用到的方法的介紹,記得最後一定要close()哦!

以上內容只代表我個人的觀點,有什麼錯誤的地方請各路大神指正!轉載請註明出處!謝謝!