Java寫文件的方式-二進制文件

前言

當大家面臨需要持久化的數據的時候,可能會面臨很多選擇,比如數據庫,文件等。博主這裏有一個需求,有很多組數據,0-4096的整型,很顯然如果使用數據庫,每一組數據作爲一條記錄,總是感覺浪費,而且這些數據,單一值的價值並不大,通常需要幾百組放到一起才有價值。所以如果將幾百組數據匯聚到一個cache文件中,那就可以解決這個問題了,但是考慮到整型,如果我們可以直接利用整型進行存儲,那是不是可以達到最高的效率呢,比如二進制文件。所以這篇博文就探討下Java中寫文件的方式。

FileWriter

FileWritter, 字符流寫入字符到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作爲FileWritter構造函數的第二個參數,它會保留現有的內容,並追加新內容在文件的末尾。

  1. 替換所有現有的內容與新的內容。

new FileWriter(file);

  1. 保留現有的內容和附加在該文件的末尾的新內容。

new FileWriter(file,true);

示例代碼:

    private static void outByFileWriter(String filePath) {
        File target = new File(filePath);
        if (target.exists() && target.isFile()){
            boolean flag = target.delete();
        }
        try {
            if (target.createNewFile()){
                for (int i = 0; i < 1024; i++) {
                    FileWriter fileWriter = new FileWriter(target.getAbsoluteFile(), true);
                    fileWriter.write(i);
                    fileWriter.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

結果:

0AgKd.png

0Atla.png

可以看到文件確實是在用二進制的形式在保存,但是利用write(int integer)的方法進行整型的保存會保存一個8位整型,並不能達到我們的需求。

BufferedWriter

緩衝字符(BufferedWriter )是一個字符流類來處理字符數據。不同於字節流(數據轉換成字節),你可以直接寫字符串,數組或字符數據保存到文件。

示例代碼:

    private static void outByBufferWriter(String filePath) {
        File target = new File(filePath);
        if (target.exists() && target.isFile()){
            boolean flag = target.delete();
        }
        try {
            if (target.createNewFile()){
                BufferedWriter out = new BufferedWriter(new FileWriter(target.getAbsoluteFile(), true));
                out.write(128);
                out.write(-128);
                out.write(129);
                out.write(-129);

                out.close();
//                for (int i = 0; i < 1024; i++) {
//                    BufferedWriter out = new BufferedWriter(new FileWriter(target.getAbsoluteFile(), true));
//                    out.write(i);
//                    out.close();
//                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

小思考

爲什麼寫入文件後,文件的內容並不是我們期待的東西呢?

public void write(int c) throws IOException 寫入單個字符。
c - 指定要寫入字符的ASCII。

FileOutputStream

文件輸出流是一種用於處理原始二進制數據的字節流類。爲了將數據寫入到文件中,必須將數據轉換爲字節,並保存到文件。

示例代碼:


    private static void outByFileDataOutputStream(String filePath) {
        File target = new File(filePath);
        if (target.exists() && target.isFile()){
            boolean flag = target.delete();
        }
        try {
            if (target.createNewFile()){
                for (int i = 0; i < 4096; i++) {
                    DataOutputStream out = new DataOutputStream(new FileOutputStream(filePath, true));
                    byte[] bytes = new byte[2];
                    bytes[0] = (byte) (i / 256);
                    bytes[1] = (byte) (i % 256);
                    out.write(bytes);
//                    out.writeInt(i);
                    out.close();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

結果:

0A1vX.md.png

0A4UG.png

最後結果達到了我們預期的效果,既保存了數據,又節省了空間,very good!

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