簡體   English   中英

randomAccessFile方法無法寫入或讀取整數?

[英]randomAccessFile method not writing or reading ints?

我有兩種方法,一種是將10個整數寫入randomAccessFile,另一種是讀取10個整數。 我相信writer方法無法正常運行。

這是我寫入隨機文件的方法:

try{
                RandomAccessFile file = new RandomAccessFile(nextPath, "rw");
                System.out.println("Writing these ints to file");
                file.seek(0);
                // here I loop 10 times and write the int at position i
                for (int i = 0; i < 10; i++)
                {
                    file.seek(i);
                    toAdd = randInt(1,10);
                    file.writeInt(toAdd);
                    System.out.printf(toAdd + " ");

                }
                file.seek(0);
                System.out.println("");
            }catch(IOException ex){
                System.out.println("Error");
                System.exit(0);}

這是我讀取int的方法:

public int[] readRandom()
    {
        System.out.println("Unsorted ints found in random file:");
        int[] randomInts = new int[10];
        try{        
                RandomAccessFile file = new RandomAccessFile(nextPath, "rw");
                file.seek(0);
                // here I loop 10 times, and read the Int at position i
                for(int i = 0; i < 10; i++)
                {
                    file.seek(i);
                    randomInts[i] = file.readInt();
                    System.out.printf(randomInts[i] + " ");


                }   
                file.seek(0);
                System.out.println("");
            }catch(IOException exc){
                System.out.println("Error reading ints");
                System.exit(0);}
                return randomInts;
            }

這是我的輸出:為什么只讀取最后一個int?

Writing these ints to file
1 4 5 6 2 4 10 6 5 5 
Unsorted ints found in random file:
0 0 0 0 0 0 0 0 0 5

在執行file.seek(i)時,您錯過了重要的一點。 seek()方法將查找您提供的字節位置,並且將要查找的位置增加1。但是,當您將整數寫入文件時,它將為每個整數寫入4個字節。 你的櫃台應該是這樣

for (int i = 0; i < 10; i++) {
    file.seek(i*4);
    toAdd = randInt(1,10);
    file.writeInt(toAdd); 
    System.out.printf(toAdd + " ");
 }

顯然,對於閱讀,您可以執行此操作

for (int i = 0; i < 10; i++)
{
    file.seek(i*4);
    randomInts[i] = file.readInt();
    System.out.printf(randomInts[i] + " ");

}

為什么要使用RandomAccessFile執行順序寫入和順序讀取? 它違背了這個階級的目的。
此外,您不應震驚捕獲的異常,而應記錄或跟蹤它們。

關於您的意外結果,這是由不必要的seek()調用引起的。 您不需要在每次讀取或寫入操作時都調用seek() ,因為readInt()writeInt()可使光標前進。

我已經簡化了示例代碼以強調重要部分:

public void write() {
    try {
        RandomAccessFile file = new RandomAccessFile("temp.txt", "rw");
        System.out.println("Writing these ints to file");
        file.seek(0);
        for (int i = 0; i < 10; i++) {              
            file.writeInt(i);
            System.out.printf(i + " ");
        }           
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(0);
    }
}

public int[] readRandom() {
    System.out.println("Unsorted ints found in random file:");
    int[] randomInts = new int[10];
    try {
        RandomAccessFile file = new RandomAccessFile("temp.txt", "rw");
        long filePointer = file.getFilePointer();

        file.seek(0);
        for (int i=0; i<10; i++){
            randomInts[i] = file.readInt();
            System.out.printf(randomInts[i] + " ");
        }               
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(0);
    }
    return randomInts;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM