2017-03-23 48 views
0

我有兩種方法,一種是將10個整數寫入randomAccessFile,另一個是讀取10個整數。我相信作家的方法沒有按預期運作。randomAccessFile方法沒有寫入或讀取整數?

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

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);} 

這裏是我的方法讀取整數:

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 

回答

1

爲什麼使用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; 
} 
1

當你在做file.seek(i)時,你錯過了一個重要的觀點。 seek()方法尋找你提供的字節位置,並且你正在增加尋找的位置。但是當你寫入整數時,它會爲每個整數寫入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] + " "); 

} 
+0

這解決了這個問題。所以如果我要寫雙打而不是整數,我會乘以8? – mark1092

+0

答案是肯定的。你可以檢查RandomAccessFile的[specification](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html) – mesbah

相關問題