2017-03-23 5 views
0

私は、randomAccessFileに10個のintを書き込むメソッドと10個のintを読み取るメソッドの2つのメソッドを持っています。私は作家の方法が意図したとおりに機能していないと信じています。ここでrandomAccessFileメソッドはintを書き込んだり読み込んだりしませんか?

はランダムなファイルへの書き込みに私の方法であり、ここで

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 

答えて

1

シーケンシャル書物とシーケンシャル読み出しを実行するためにRandomAccessFileを使用して、なぜ?それはこのクラスの目的を破るものです。
さらに、キャッチされた例外にはショックを与えず、ログに記録したりトレースしたりしないでください。

予期せぬ結果については、必要でない場合は、seek()の呼び出しが原因です。 readInt()writeInt()は、それぞれの読み取りまたは書き込み操作でseek()を呼び出す必要はありません。

私は重要な部分強調し、あなたのサンプルコードを単純化しています

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()メソッドは、指定したバイト位置を求めて、シークする位置を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] + " "); 

} 
+0

、それを解決しました。だから、intの代わりにdoubleを書くと、iを8倍しますか? – mark1092

+0

答えははいです。 RandomAccessFileの[仕様](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html)を確認することができます。 – mesbah

関連する問題