Java I/O系統學習系列三:I/O流的典型使用方式


  盡管可以通過不同的方式組合IO流類,但我們可能也就只用到其中的幾種組合。下面的例子可以作為典型的IO用法的基本參考。在這些示例中,異常處理都被簡化為將異常傳遞給控制台,但是這只有在小型示例和工具中才適用。在代碼中,你需要考慮更加復雜的錯誤處理方式。

  同樣,本文會包括如下幾個方面:

  緩沖輸入文件

  從內存輸入

  格式化的內存輸入

  基本的文件輸出

  存儲和恢復數據

  讀寫隨機訪問文件

  實用工具

  總結

 

1. 緩沖輸入文件

  如果想要打開一個文件用於字符輸入,可以使用以String或File對象作為文件名的FileReader。為了提高速度,我們可以對那個文件進行緩沖,那么我們需要將所產生的引用傳給一個BufferedReader構造器。通過使用其readLine()方法來逐行讀取文件,當readLine()返回null時,就到了文件末尾。

public class BufferedInputFile {

    public static String read(String fileName) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        StringBuilder str = new StringBuilder();
        String temp = null;
        while((temp = br.readLine()) != null) {
            str.append(temp + "\n");
        }
        br.close();
        return str.toString();
    }
    
    public static void main(String[] args) {
        try {
            System.out.println(BufferedInputFile.read("pom.xml"));
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

  文件的全部內容都累積在字符串str中,最后記得調用close()來關閉流。

2. 從內存輸入

  在下面的示例中,從上面的BufferedInputFile.read()讀入的String結果被用來創建一個StringReader。然后調用read()每次讀取一個字符,並把它打印到控制台。

public class MemoryInput {
    
    public static void main(String[] args) {
        try {
            StringReader sr = new StringReader(BufferedInputFile.read("pom.xml"));
            int c;
            while((c = sr.read()) != -1) {
                System.out.print((char)c);
            }
        }catch(Exception e) {
            
        }
    }

}

  需要注意的是read()是以int形式返回下一個字節,因此必須將類型強轉為char才能顯示正確結果。

3. 格式化的內存輸入

  要讀取格式化數據,可以使用DataInputStream,它是一個面向字節的I/O類,我們可以用InputStream以字節的形式讀取任何數據。

public class FormattedMemoryInput {

    public static void main(String[] args) {
        try {
            DataInputStream di = new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("pom.xml").getBytes()));
            while(di.available() != 0) {
                System.out.print((char)di.readByte());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  這里需要注意必須為ByteArrayInputStream的構造函數提供字節數組,而ByteArrayInputStream傳遞給DataInputStream之后進行了一次“裝飾”,可以進行格式化輸入(比如直接讀取int、double等類型),這里我們只是通過readByte讀取單個字節,調用該方法時任何字節的值都是合法的結果,因此返回值是不能用來檢測輸入是否結束,這里我們使用available()方法查看還有多少可供存取的字符來判斷是否結束。

4. 基本的文件輸出

  FileWriter對象可以向文件寫入數據,通常會用BufferedWriter將其包裝起來用以緩沖輸出以提高性能。在本例中為了提供格式化機制,將其裝飾成PrintWriter:

public class BasicFileOutput {
    
    static String file = "BasicFileOutput.out";
    
    public static void main(String[] args) {
        try {
            BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read("pom.xml")));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            String temp;
            int count = 0;
            while((temp = in.readLine()) != null) {
                out.println(count++ + temp);
            }
            in.close();
            out.close();
            System.out.println(BufferedInputFile.read(file));
        }catch(Exception e) {
            e.printStackTrace();
        }        
    }

}

  這里在讀取BasicFileOutput.out的內容之前,先調用了out的close()將其關閉,一方面是因為流用完之后需要及時關閉以節省資源,另一方面這里用到了緩沖區,如果不為所有的輸出文件調用close(),緩沖區的內容可能不會刷新清空,這樣可能導致信息不完整。

  另外Java SE5在PrintWriter中添加了一個輔助構造器,可以很方便根據文件名直接構造一個PrintWriter而不用執行一系列的裝飾工作:

PrintWriter out = new PrintWriter(file);

5. 存儲和恢復數據

  PrintWriter可以對數據進行格式化,以便閱讀。但是為了輸出可供另一個“流”恢復的數據,我們需要用DataOutputStream寫入數據,並用DataInputStream恢復數據。當然,這些流可以是任何形式,在下面的例子中使用的是一個文件。

public class StoringAndRecoveringData {
    
    public static void main(String[] args) {
        try {
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
            out.writeDouble(3.14159);
            out.writeUTF("That was pi");
            out.writeDouble(1.41413);
            out.writeUTF("Square root of 2");
            out.close();
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data.txt")));
            System.out.println(in.readDouble());
            System.out.println(in.readUTF());
            System.out.println(in.readDouble());
            System.out.println(in.readUTF());
            in.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

  使用DataOutputStream寫入數據,Java可以保證我們可以使用DataInputStream准確地讀取數據--無論讀和寫數據的平台多么不同。當我們使用DataOutputStream時,寫字符串並且讓DataInputStream能夠恢復它的唯一可靠的做法就是使用UTF-8編碼,在這個例子中是靠writeUTF()和readUTF()來實現的。

  writeDouble()和readDouble()方法能夠寫入和恢復double類型的數據。對於其他類型的數據,也有類似的方法用於讀寫。但是為了保證所有的讀寫方法都能夠正常工作,我們必須知道流中數據項所在的確切位置,因為極有可能將保存的double數據作為一個簡單的字節序列、char或其他類型讀入。

6. 讀寫隨機訪問文件

  使用RandomAccessFile,類似於組合使用了DataInputStream和DataOutputStream,可以同時對一個文件執行讀寫操作,同時可以利用seek()在文件中到處移動,非常方便,關於RandomAccessFile的詳細用法,前面有專門寫過<<Java I/O系統:File和RandomAccessFile>>。

  但是在使用RandomAccessFile時,你需要知道文件的排版,這樣才能正確地操作它,RandomAccessFile擁有讀取基本類型和UTF-8字符串的各種具體方法。

public class UsingRandomAccessFile{
    static String file = "rtest.dat";
    static void display() throws IOException{
        RandomAccessFile rf = new RandomAccessFile(file,"r");
        for(int i = 0; i < 7; i++){
            System.out.println("Value " + i + ": " + rf.readDouble());
        }
        System.out.println(rf.readUTF());
        rf.close();
    }
    public static void main(String[] args) throws IOException{
        RandomAccessFile rf = new RandomAccessFile(file,"rw");
        for(int i = 0; i < 7; i++){
            rf.writeDouble(i*1.414);
        }
        rf.writeUTF("The end of the file");
        rf.close();
        display();
        rf = new RandomAccessFile(file,"rw");
        rf.seek(5*8);
        rf.writeDouble(47.0001);
        rf.close();
        display();
    }
}

  我們通過writeDouble()方法往文件中寫入Double類型數據並通過readDouble()方法來讀取,這就是我們需要直到排版的原因,如果讀取的不是Double類型的數據有可能出現不是我們想要的結果。

7. 實用工具

  到這里我們學習了多種I/O流的典型用法,比如緩沖輸入文件、從內存輸入、基本的文件輸出、存儲和恢復數據、隨機讀寫文件,這些都是Java I/O流比較典型的用法。這里我們發現讀取文件、修改、在寫出是一個很常見的程序化的任務,但是Java I/O類庫的設計有一個問題,就是我們需要編寫很多代碼來實現這些操作,要記住如何打開文件是一件優點困難的事情。因此,下面是收集的一些幫助類,可以很容易為我們完成這些基本任務,記錄在這里,方便以后查看。

  這里收集了兩個工具:

  • 一個是TextFile,幫助我們讀取和寫入文件;
  • 另一個是BinaryFile,幫助我們簡化二進制文件的讀取。

7.1 讀取文件

  TextFile類包含的static方法可以像簡單字符串那樣讀寫文本文件,並且我們可以創建一個TextFile對象,它用一個ArrayList來保存文件的若干行,好處是在我們操縱文件內容時可以使用ArrayList的所有功能。

public class TextFile extends ArrayList<String>{
    // 將文件讀取到一行字符串中
    public static String read(String fileName){
        StringBuilder sb = new StringBuilder();
        try{
            BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsoluteFile()));
            try{
                String s;;
                while((s = in.readLine()) != null){
                    sb.append(s).append("\n");
                }
            }finally{
                in.close();
            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

    // 單次調用將一個字符串寫入一個文件
    public static void write(String fileName,String text){
        try{
            PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
            try{
                out.print(text);
            }finally{
                out.close();
            }
        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }

    // 讀取文件,並通過正則表達式將其分離,保存在List中
    public TextFile(String fileName,String splitter){
        super(Arrays.asList(read(fileName).split(splitter)));
        // 因為split()方法有時會在返回的數組第一個位置產生一個空字符串
        if(get(0).equals(""))
            remove(0);
    }

    // 常規的分行讀取
    public TextFile(String fileName){
        this(fileName,"\n");
    }

    // 將該TextFile中的內容分行寫入指定文件中
    public void write(String fileName){
        try{
            PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
            try{
                for(String item : this){
                    out.println(item);
                }
            }finally{
                out.close();
            }
        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }

    // 簡單驗證一下
    public static void main(String[] args){
        String file = read("TextFile.java");
        write("test.txt",file);
        TextFile text = new TextFile("test.txt");
        text.write("test2.txt");
        TreeSet<String> words = new TreeSet<String>(new TextFile("TextFile.java","\\W+"));
        System.out.println(words.headSet("a"));
    }
}

  這里利用靜態的read()方法將文件讀取到一個字符串中,再用靜態的write()方法將其寫入到文件中。然后將新寫入的文件作為構造參數構造一個TestFile對象,利用其List的特性,將其內容寫入文件test2中。這個類的作用是幫我們讀取文件,可以通過靜態的read方法讀取到一個字符串中,也可以通過構造器讀取文件到一個TextFile對象中。

7.2 讀取二進制文件

public class BinaryFile{
    public static byte[] read(File bFile)throws IOException{
        BufferedInputStream bf = new BufferedInputStream(new FileInputStream());
        try{
            byte[] data = new byte[bf.available()];
            br.read(data);
            return data;
        }finally{
            bf.close();
        }
    }

    public static byte[] read(String bFile)throws IOException{
        return read(new File(bFile).getAbsoluteFile());
    }
}

8. 總結

  本文沒有總結什么新的知識點,只是總結了一些Java I/O的常見用法比如緩沖輸入文件、從內存輸入、基本的文件輸出、存儲和恢復數據、隨機讀寫文件等,並且搜集了兩個工具類用來幫助我們讀寫文件讀取二進制文件,以提高些代碼的效率。

 


免責聲明!

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



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