首頁 > Java > java教程 > 主體

隨機存取文件

王林
發布: 2024-07-18 19:08:51
原創
950 人瀏覽過

Java 提供了 RandomAccessFile 類,允許在檔案中的任何位置讀取和寫入資料。到目前為止,您使用過的所有流都稱為只讀只寫流。這些流稱為順序流。使用順序流開啟的檔案稱為順序存取檔案。順序存取文件的內容無法更新。然而,經常需要修改文件。 Java 提供了 RandomAccessFile 類別來允許在檔案中的任何位置讀取和寫入資料。使用 RandomAccessFile 類別開啟的檔案稱為 隨機存取檔案

RandomAccessFile 類別實作了 DataInputDataOutput 接口,如下圖所示。 DataInput 介面定義了讀取原始型別值和字串的方法(例如,readIntreadDoublereadChar readBooleanreadUTF)和DataOutput 介面定義了寫入原始型別值和字串的方法(例如,writeIntwriteDoublewriteCharwriteBooleanwriteUTF)。

Image description

建立 RandomAccessFile 時,您可以指定以下兩種模式之一:rrw。模式 r 表示流是唯讀的,模式 rw 表示流允許讀取和寫入。例如,以下語句建立一個新流 raf,該流允許程式讀取和寫入檔案 test.dat:

RandomAccessFile raf = new RandomAccessFile("test.dat", "rw");

如果test.dat已經存在,則建立raf來存取它;如果test.dat 不存在,則會建立一個名為test.dat 的新文件,並建立raf 來存取新文件。方法 raf.length() 傳回任意給定時間 test.dat 中的位元組數。如果將新資料追加到檔案中,raf.length() 會增加。

如果不想修改文件,請使用 r 模式來開啟它。這可以防止無意中修改文件。

隨機存取檔案由位元組序列組成。稱為檔案指標的特殊標記位於這些位元組之一。讀取或寫入操作發生在文件指標的位置。開啟檔案時,檔案指標會設定在檔案的開頭。當您向檔案讀取或寫入資料時,檔案指標會向前移動到下一個資料項目。例如,如果您使用readInt() 讀取int 值,則JVM 從檔案指標讀取4 位元組,現在檔案指標為4 個位元組,如下圖所示。

Image description

對於

RandomAccessFile raf,可以使用 raf.seek(position) 方法將檔案指標移到指定位置。 raf.seek(0) 將其移至檔案的開頭,raf.seek(raf.length()) 將其移至檔案的末端。下面的程式碼示範了 RandomAccessFile.

package demo;
import java.io.*;

public class TestRandomAccessFile {

    public static void main(String[] args) throws IOException {
        try( // Create a random access file
            RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
        ) {
            // Clear the file to destroy the old contents if exists
            inout.setLength(0);

            // Write new integers to the file
            for(int i = 0; i < 200; i++)
                inout.writeInt(i);

            // Display the current length of the file
            System.out.println("Current file length is " + inout.length());

            // Retrieve the first number
            inout.seek(0); // Move the file pointer to the beginning
            System.out.println("The first number is " + inout.readInt());

            // Retrieve the second number
            inout.seek(1 * 4); // Move the file pointer to the second number
            System.out.println("The second number is " + inout.readInt());

            // Retrieve the tenth number
            inout.seek(9 * 4); // Move the file pointer to the tenth number
            System.out.println("The tenth number is " + inout.readInt());

            // Modify the eleventh number
            inout.writeInt(555);

            // Append a new number
            inout.seek(inout.length()); // Move the file pointer to the end
            inout.writeInt(999);

            // Display the new length
            System.out.println("The new length is " + inout.length());

            // Retrieve the new eleventh number
            inout.seek(10 * 4); // Move the file pointer to the eleventh number
            System.out.println("The eleventh number is " + inout.readInt());
        }
    }

}

登入後複製
目前檔案長度為 800

第一個數字是 0
第二個數字是 1
第十個數字是 9
新的長度是804
第11個數字是555

為名為

inout.dat 的檔案建立一個RandomAccessFile,模式為rw,以允許第8 行中的讀取和寫入操作。

inout.setLength(0) 將第 11 行中的長度設定為 0。這實際上會破壞文件的舊內容。

for 循環將 200 個 int 值從 0199 寫入檔案第 14 和 15 行。 🎜>int 值需要4 個位元組,從inout.length() 傳回的檔案總長度現在是800 (第18 行) ,如範例輸出所示。

第 21 行呼叫 inout.seek(0) 將檔案指標設定為檔案的開頭。 inout.readInt() 讀取第 22 行中的第一個值並將檔案指標移到下一個數字。第二個數字在第 26 行讀取。

inout.seek(9 * 4)(第 29 行)將檔案指標移到第十個數字。 inout.readInt() 讀取第十個數字,並將檔案指標移到第 30 行的第十一個數字。 inout.write(555) 在目前位置寫入新的第十一個數字 (第 33 行)。之前的第十一個數字被銷毀。

inout.seek(inout.length()) 將檔案指標移到檔案結尾(第 36 行)。 inout.writeInt(999)999 寫入檔案(第 37 行)。現在檔案的長度增加了 4,因此 inout.length() 回傳 804(第 40 行)。

inout.seek(10 * 4) 將檔案指標移到第 43 行中的第十一個數字。新的第十一個數字 555 顯示在第 44 行。

以上是隨機存取文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!