从randomacces文件写入/读取

时间:2017-10-25 02:17:16

标签: java randomaccessfile eofexception

我有一个项目,我将数据(字符串和整数)写入二进制随机访问文件,并在单独的类中读取数据。我遇到的问题是我试图遍历文件并按特定顺序读取数据(int,String,String,int),但字符串是各种字节大小。

我收到了EOFException,但无法弄清楚原因。

这是写入数据的类。部分要求是限制字符串的字节数,并在超出时捕获用户定义的异常。

import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.File;

public class QuestionBank {
   private RandomAccessFile file;
   private ArrayList <Questions> listQuestions;

   public QuestionBank(){
      file = null;
      listQuestions = new ArrayList<Questions>();      
   }

   public void storeQuestion (Questions ques) throws IOException {

      ques = new Questions(ques.getQuesIDNum(), ques.getQuestion(), ques.getAnswer(), ques.getValue());

      listQuestions.add(ques);

      byte[] quesBytes = ques.getQuestion().getBytes("UTF-8");
      byte[] ansBytes = ques.getAnswer().getBytes("UTF-8");

      try {
           file = new RandomAccessFile(new File("Question.bin"), "rw");

           long fileSize = file.length();

           file.seek(fileSize);

           file.writeInt(ques.getQuesIDNum());

           file.writeUTF(ques.getQuestion());

           for (int i = 0; i <= 50 - ques.getQuestion().length(); i++){
              file.writeByte(50);
              }              
           if (quesBytes.length > 50) {
              throw new ByteSizeException("Question has too many bytes");
              }

           file.writeUTF(ques.getAnswer());

           for (int i = 0; i <= 20 - ques.getAnswer().length(); i++){
              file.writeByte(20);
              }
           if (ansBytes.length > 20) {
              throw new ByteSizeException("Answer has too many bytes");
              }

           file.writeInt(ques.getValue()); 

           file.close();
          } catch (IOException e) {

            System.out.println("I/O Exception Found");


          } catch (ByteSizeException eb) {

            System.out.println("String has too many bytes");
          }                
    }

这是读取文件的类。

import java.util.ArrayList;
import java.util.Random;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.io.File;

public class TriviaGame {
   public static final int RECORD = 78;
   private ArrayList<Questions> quesList;
   private int IDNum;
   private String question;
   private String answer;
   private int points;

   public TriviaGame() {
      quesList = new ArrayList<Questions>();
      IDNum = 0;
      question = "";
      answer = "";
      points = 0;
   }

   public void read(){ 
      try {
          RandomAccessFile file;

          file = new RandomAccessFile(new File("Question.bin"), "r");
          long fileSize = file.length();        
          long numRecords = fileSize/RECORD;

          file.seek(0);

          for (int i = 0; i < numRecords; i++){
             IDNum = file.readInt();
             question = file.readUTF();                                       
             answer = file.readUTF();                                              
             points = file.readInt();

             System.out.println("ID: " + IDNum + " Question: " + question + " Answer: " + answer + " Points: " + points);
         }
     file.close();

     } catch (IOException e) {
       System.out.println(e.getClass());
       System.out.println("I/O Exception found");   
    }  
  } 
}

由于

1 个答案:

答案 0 :(得分:0)

file.writeUTF(ques.getQuestion());

在这里你写了这个问题。

for (int i = 0; i <= 50 - ques.getQuestion().length(); i++){
    file.writeByte(50);
}              
if (quesBytes.length > 50) {
    throw new ByteSizeException("Question has too many bytes");
}

由于某些未知原因,您将问题填充到50个字节。去掉。与答案相同。您正在使用readUTF()来阅读这两者,因此您需要writeUTF()来编写它们。不需要填充。

,如果你坚持这个填充,你必须在阅读时跳过它:在第一个readUTF()之后,你需要跳过填充。