天天看點

java IO流(一)java IO流

java IO流

流是一組有順序的,有起點和終點的位元組集合,是對資料傳輸的總稱或抽象。即資料在兩裝置間的傳輸稱為流,流的本質是資料傳輸,根據資料傳輸特性将流抽象為各種類,友善更直覺的進行資料操作。

IO流的分類

根據處理資料類型:位元組流和字元流
根據資料流向:輸入流和輸出流
           

位元組流和字元流

位元組流:以位元組為機關,能處理所有類型的資料
字元流:以字元為機關,處理文本類型的資料
           

輸入流和輸出流

輸入流:隻能進行讀操作
輸出流:隻能進行寫操作
           

java IO流對象

  • 輸入位元組流 InputStreamIO:

    通過InputStream從檔案中把内容讀取進來,首先來看InputStream類的定義:

    public abstract class InputStream extends Object implements Closeable

    InpubStreamIO也是一個抽象類必須依靠其子類才能執行個體化如果是從檔案裡面讀取,就用FileInputStream來實作。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * 
 * @ClassName: FileInputStreamTest 
 * @Description: 讀取檔案内容
 * @author LiuHuan
 * @date 2017-8-9 下午3:08:05
 */
public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        //得到File檔案
        File file = new File("G:"+File.separator+"Demo.txt");
        //通過File得到輸入流
        InputStream in = new FileInputStream(file);
        //位元組數組
        byte[] b =new byte[];
        //讀
        int len = in.read(b);
        //關閉流
        in.close();
        //輸出内容
        System.out.println(new String( b, , len));

    }
}
           
但我們不用開辟這麼大的一個位元組數組,我們可以根據檔案的大小來定義位元組數組的大小,File類中的方法:public long length()
           
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest1 {
    public static void main(String[] args) throws IOException {
        //通過指定路徑得到File
        File file = new File("G:" + File.separator + "Demo.txt");
        //通過指定File得到輸入流
        InputStream in = new FileInputStream(file);
        //設定為合理的位元組數組
        byte[] b = new byte[(int) file.length()];
        //輸入到位元組數組b中
        in.read(b);
        //關閉流
        in.close();
        //輸出結果
        System.out.println(new String(b));
    }
}
           

另一種讀取方式:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest2 {
    public static void main(String[] args) throws IOException {
        //通過指定的路徑得到File
        File file = new File("G:" + File.separator + "Demo.txt");
        //通過指定的File得到InputStream
        InputStream in = new FileInputStream(file);
        //擷取到合适的位元組數組
        byte[] b = new byte[(int) file.length()];
        //一個個位元組讀取
        for(int i = ; i < b.length; i++){
            b[i] = (byte) in.read();
        }
        //關閉流
        in.close();
        //輸出結果
        System.out.println(new String(b));
    }
}
           

不知檔案大小用以下方法:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest3 {
    public static void main(String[] args) throws IOException {
        //通過指定路徑擷取File
        File file = new File("G:" + File.separator + "Demo.txt");
        //通過指定File擷取InputStream
        InputStream in = new FileInputStream(file);
        //定義位元組數組
        byte[] b = new byte[];

        int temp = ;

        int len =;
        //讀取資料放入b中
        while((temp = in.read()) != -){
            b[len] = (byte) temp;
            len++;
        }
        //關閉流
        in.close();
        //輸出結果
        System.out.println(new String( b, , len));
    }
}
           
  • 輸出位元組流 OutputStreamIO:

    既然程式可以向檔案讀取内容,則就可以向檔案寫内容首先看OutputStreamIO定義:

    public abstract class OutputStreamextends Objectimplements Closeable, Flushable

    OutpubStreamIO也是一個抽象類必須依靠其子類才能執行個體化如果是從檔案裡面寫入,也用FileOutputStream來實作。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileOutputStreamTest {
    public static void main(String[] args) throws IOException {
        //向檔案寫入的資料
        String data = "Hello_Word";
        //指定寫入的檔案路徑
        File file = new File("G:" + File.separator +"Test.txt");
        //擷取輸出流
        OutputStream os = new FileOutputStream(file);
        //資料轉換為byte位元組數組寫入到檔案
        os.write(data.getBytes());
        //關閉流
        os.close();
    }
}
           
  • 輸出字元流 Writer:

    Writer屬于抽象類,這邊是可以直接寫入字元資料,不需要進行轉換操作。我們這邊用FileWriter來舉例:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class FileWriterTest {
    public static void main(String[] args) throws IOException {
        //寫入檔案的資料
        String data = "Hello_Word!";
        //擷取資料寫入的路徑
        File file = new File("G:" + File.separator + "Test.txt");
        //擷取輸出字元流
        Writer writer = new FileWriter(file);
        //寫入資料
        writer.write(data);
        //關閉流
        writer.close();

    }
}
           

在預設的情況下會清空原來的資料,如需追加資料使用如下方法:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class FileWriterTest {
    public static void main(String[] args) throws IOException {
        //寫入檔案的資料
        String data = "Hello_Word!";
        //擷取資料寫入的路徑
        File file = new File("G:" + File.separator + "Test.txt");
        //擷取輸出字元流
        Writer writer = new FileWriter(file, true);
        //寫入資料
        writer.write(data);
        //關閉流
        writer.close();

    }
}
           

相信大家也看到了,這邊隻需在Writer writer = new FileWriter(file, true);就可以追加資料。

  • 字元輸入流 Reader

    Reader是從檔案裡面讀取資料,也是抽象類。

    下面我們用FileReader來舉例:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderTest {
    public static void main(String[] args) throws IOException {
        //檔案的路徑
        File file = new File("G:" + File.separator + "Test.txt");
        //擷取輸入流
        Reader reader = new FileReader(file);
        //字元數組
        char[] c = new char[];
        //讀取資料放置字元數組
        int read = reader.read(c);
        //關閉流
        reader.close();

        System.out.println(new String(c, , read));
    }
}
           

這樣的話是不是感覺浪費資源了呢?那我們看下面:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderTest1 {
    public static void main(String[] args) throws IOException {
        //檔案的路徑
        File file = new File("G:" + File.separator + "Test.txt");
        //輸入流
        Reader reader = new FileReader(file);
        //字元數組
        char[] c = new char[(int) file.length()];//根據檔案的大小來建立字元數組
        //讀取資料
        reader.read(c);
        //關閉流
        reader.close();
        //輸出結果
        System.out.println(new String(c));
    }
}
           

如果不知道檔案大小也可以用以下方法:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderTest2 {
    public static void main(String[] args) throws IOException {
        //檔案的路徑
        File file = new File("G:" + File.separator + "Test.txt");
        //字元數組
        char[] c = new char[];
        //輸入流
        Reader reader = new FileReader(file);
        //讀取資料
        int temp= ;
        int len = ;
        while((temp = reader.read()) != -){
            c[len] = (char) temp;
            len++;
        }
        //關閉流
        reader.close();
        //輸出結果
        System.out.println(new String(c));
    }
}
           

位元組流與字元流的差別

通過以上的情況我們不難發現位元組流和字元流的差別:

位元組流在操作的時候本身是不會用到緩沖區(記憶體)的,是與檔案本身直接操作的,而字元流在操作的時候是使用到緩沖區的

位元組流在操作檔案時,即使不關閉資源(close方法),檔案也能輸出,但是如果字元流不使用close方法的話,則不會輸出任何内容,說明字元流用的是緩沖區,并且可以使用flush方法強制進行重新整理緩沖區,這時才能在不close的情況下輸出内容