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

Java 如何从随机访问文件中获取信息并将其放入多个数组中?,java,fileinputstream,randomaccessfile,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 th

我搞不清楚这件事。我应该从提供给我的随机访问文件中读取信息,然后将信息放入几个数组(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;
    }
这会产生一个EOFEException,我不知道还有什么可以工作。
注意:count和PrintArray指的是程序的另一部分,当我将信息放入数组时,它的运行方式与我编写的另一个程序类似,而我不知道文件的格式,我不能更具体地说,但一般来说,你的问题是你在做一个inputFile.read()每次循环并检查返回值-然后继续从输入文件中读取更多内容,而不检查是否有更多内容要读取,也不对第一次读取的数据执行任何操作

如果行像CSV文件一样排列,那么最好的方法是使用readLine并以字符串的形式一次读取一行文件

然后对字符串使用split()将行中的数据按任何分隔符进行分割

然后将数组split()返回的元素读取到数据数组中


还要注意的是,您并没有检查数据数组的大小,只是将它们填满,并希望读取的数据少于100行。对于较大的文件,该操作将失败。

为此,已指定不超过100个元素。我不知道该文件是如何作为.dat文件而不是文本来布局的。对于书中、课堂上或任何演示文稿中未指定的内容,很难找到解决方案。请在记事本或类似文件中打开.dat文件(即使它不是.txt,但仍会读取它,如果它的文本数据显示它)。