我如何从随机访问文件中获取信息并将其放入多个数组中?

时间:2014-02-23 00:58:46

标签: java fileinputstream randomaccessfile

我在解决这个方面遇到了一些麻烦。我应该从提供给我的随机访问文件中读取信息,然后将信息放入几个数组(firstName,lastName,age,gpa,computerClub)。有人告诉我,每种类型的信息都写成了什么,但不是按什么顺序写的。如何读取此信息并将其放入适当的数组中。这就是我到目前为止所做的:

import java.io.*;//imports the IO tools for use in this program
import java.text.DecimalFormat;//imports the decimal format tool for use in this program
public class RandomAccessProject
{
   public static void main(String[] args)throws Exception
   {

      String[] firstName = new String[100];//creates an array of length 100 called firstName
      String[] lastName = new String[100];//creates an array of length 100 called lastName
      int[] age = new int[100];//creates an array of length 100 called age
      double[] gpa = new double[100];//creates an array of length 100 called gpa
      boolean[] computerClub = new boolean[100];//creates an array of length 100 called computerClub   

      int numElements = loadArrays(firstName,lastName,gpa,age,computerClub);//invokes the loadArrays method to fill the created arrays with values from the text file.
      printArrays(firstName,lastName,gpa,age,computerClub,numElements);


   }


   /*
   * loadArrays
   * @param String[] first ,String[] last,double[] grades, int[] age, boolean[] club.
   * @return int count
   */
  public static int loadArrays(String[] first, String[] last, double[] grades,int[] age, boolean[] club) throws Exception
  {
      RandomAccessFile inputFile = new RandomAccessFile("rand.dat","r");//creates a new File object
      int count = 0;//creates and initializes a variable for dermining which element of an array is interacted with
      int r = 0;

      while((r = inputFile.read()) != -1)
      {
          String firstName = inputFile.readUTF();//takes the next string and places it into the firstName variable
          String lastName = inputFile.readUTF();//takes the next string and places it into the lastName variable
          int old = inputFile.readInt();
          double gpa = inputFile.readDouble();//takes the next double and places it into the score variable
          boolean compClub = inputFile.readBoolean();
          first[count] = firstName;//sets the element corresponding to the current value of count in the first array to the firstName array
          last[count] = lastName;//sets the element corresponding to the current value of count in the last array to the lastName array
          age[count] = old;
          grades[count] = gpa;//sets the element corresponding to the current value of count in the first array to the score array
          club[count] = compClub;
          count++;//increment count by one

      }
      return count;
    }

这会产生一个EOFException,我不知道还有什么可行。 注意:count和printArrays指的是程序的另一部分,它将与我在将数据输入数组后编写的另一个程序类似地操作

1 个答案:

答案 0 :(得分:0)

不知道文件的格式我不能更具体 - 但一般来说你的问题是你每次在循环周围做一个inputFile.read()并检查返回 - 但是你去了on并从输入文件中读取更多内容而不检查是否有更多内容需要阅读,而且不会对您第一次读取的数据执行任何操作。

如果该行的布局类似于CSV文件,那么最好的方法是使用readLine并一次读取文件中的一行字符串。

然后在字符串上使用split()来分隔行上的数据。

然后从数组中读取元素split()返回数据数组。

另请注意,您没有检查数据数组的大小,只是填写它们并希望您读取少于100行。对于较大的文件,这将失败。