Java學些教程:【轉載】IO流讀取數據

轉載摘自速學堂 IO技術

示例

import java.io.*;
public class TestIO2 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("d:/a.txt"); // 內容是:abc
            StringBuilder sb = new StringBuilder();
            int temp = 0;
            //當temp等於-1時,表示已經到了文件結尾,停止讀取
            while ((temp = fis.read()) != -1) {
                sb.append((char) temp);
            }
            System.out.println(sb);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //這種寫法,保證了即使遇到異常情況,也會關閉流對象。
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

結果

在這裏插入圖片描述

部分流

1. InputStream/OutputStream

    字節流的抽象類。

 2. Reader/Writer

    字符流的抽象類。

 3. FileInputStream/FileOutputStream

    節點流:以字節爲單位直接操作“文件”。

 4. ByteArrayInputStream/ByteArrayOutputStream

    節點流:以字節爲單位直接操作“字節數組對象”。

 5. ObjectInputStream/ObjectOutputStream

    處理流:以字節爲單位直接操作“對象”。

 6. DataInputStream/DataOutputStream

    處理流:以字節爲單位直接操作“基本數據類型與字符串類型”。

 7. FileReader/FileWriter

    節點流:以字符爲單位直接操作“文本文件”(注意:只能讀寫文本文件)。

 8. BufferedReader/BufferedWriter

    處理流:將Reader/Writer對象進行包裝,增加緩存功能,提高讀寫效率。

 9. BufferedInputStream/BufferedOutputStream

    處理流:將InputStream/OutputStream對象進行包裝,增加緩存功能,提高 讀寫效率。

 10. InputStreamReader/OutputStreamWriter

 	 處理流:將字節流對象轉化成字符流對象。

 11. PrintStream
		
	 處理流:將OutputStream進行包裝,可以方便地輸出字符,更加靈活。

字符流和字節流

字節流不能很好的處理Unicode字符,經常會出現“亂碼”現象。所以,我們處理文本文件,一般可以使用文件字符流,它以字符爲單位進行操作。

1、字節流FileInputStream與 FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileOutputStream {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        String string = "北京尚學堂歡迎您!";
        try {
            // true表示內容會追加到文件末尾;false表示重寫整個文件內容。
            fos = new FileOutputStream("d:/a.txt", true);
            //該方法是直接將一個字節數組寫入文件中; 而write(int n)是寫入一個字節
            fos.write(string.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2、字符流FileReader與FileWriter
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestFileCopy2 {
    public static void main(String[] args) {
        // 寫法和使用Stream基本一樣。只不過,讀取時是讀取的字符。
        FileReader fr = null;
        FileWriter fw = null;
        int len = 0;
        try {
            fr = new FileReader("d:/a.txt");
            fw = new FileWriter("d:/d.txt");
            //爲了提高效率,創建緩衝用的字符數組
            char[] buffer = new char[1024];
            //邊讀邊寫
            while ((len = fr.read(buffer)) != -1) {
                fw.write(buffer, 0, len);
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

在這裏插入圖片描述
在這裏插入圖片描述

3、緩衝字節流BufferedInputStream和BufferedOutputStream
4、緩衝字符流BufferedReader和BufferedWriter
5、字節數組流ByteArrayInputStream和ByteArrayOutputStream

經常用在需要流和數組之間轉化

6、數據流DataInputStream和DataOutputStream

提供了可以存取與機器無關的所有Java基礎類型數據(如:int、double、String等)的方法

7、對象流ObjectInputStream/ObjectOutputStream

以“對象”爲數據源,但是必須將傳輸的對象進行序列化與反序列化操作。
把Java對象轉換爲字節序列的過程稱爲對象的序列化。把字節序列恢復爲Java對象的過程稱爲對象的反序列化。
(1)ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數指定的obj對象進行序列化,把得到的字節序列寫到一個目標輸出流中。
(2)ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節序列,再把它們反序列化爲一個對象,並將其返回。

8、轉換流InputStreamReader/OutputStreamWriter

用來實現將字節流轉化成字符流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章