Java - 将文件中的数据存储到数组中

时间:2014-04-22 07:57:23

标签: java arrays try-catch bufferedreader filereader

我有我的程序来读取文件,甚至显示文件信息,但我需要知道如何将文件上的数据存储到二维数组和一维数组中。

读取的文件如下所示: 1000,亚当斯,Olivi的,80,78,61,91,20,18,18,20,19,20,19,20,20,19,20,BUS,3.1 1001,史密斯,金佰利,70,68,53,72,20,20,18,20,18,18,20,19,20,19,18,IMD,3.5 1002,Fenandez,阿龙,76,67,72,99,18,20,20,19,19,19,19,19,19,19,20,BIO,2.7

二维数组必须包含名称和三个字母之间的所有数字。

一维数组必须在最后保存GPA。

我的问题再一次是:我如何获取文件将信息存储到数组中?

import java.io.*;


public class StudentGrades
{
    public static void main(String[] args)
    {

        int[][] studentScores = new int[93][15];

        float[] studentGPA = new float[93];

        String fileName = "Students.txt";


        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;

            while((line = bufferedReader.readLine()) != null)
                System.out.println(line);
                }
            catch(IOException ex){
            System.out.println("error");
        }

    }
}

4 个答案:

答案 0 :(得分:1)

如果您确定文件中的输入始终采用相同的格式(即每行的数据量相同,并且使用此序列):

public static void main(String[] args) {

        int[][] studentScores = new int[93][15];

        float[] studentGPA = new float[93];

        String fileName = "Students.txt";

        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;

            int studentIndex = 0;
            String[] lineSplit;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                lineSplit = line.split(",");
                for (int k = 0; k < 15; k++) {
                    studentScores[studentIndex][k] = Integer.parseInt(lineSplit[3 + k]);
                }
                studentGPA[studentIndex] = Float.parseFloat(lineSplit[lineSplit.length - 1]);
                studentIndex++;
            }
        } catch (IOException ex) {
            System.out.println("error");
        }

    }

答案 1 :(得分:0)

如果文件中的数据可以有不同的长度,最好的方法是使用正则表达式来获取这些数据。 (http://regexpal.com/ + http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

但是,如果您只需要一个简单的拆分等,每行只需要split方法 - 那么您将拥有一个包含所有拆分值的数组。 (How to split a String in Java

答案 2 :(得分:0)

将变量count初始化为零,并在每次从文件中读取一行时将其递增。这可以是你对数组的索引。

使用String.split方法使用逗号字符拆分每一行。然后,使用循环将此数据插入到正确的数组中,例如:

String[] parts = input.split(",");
for (int i = 0; i < 15; i++) {
  studentScores[count][i] = parts[i+3];
}

答案 3 :(得分:0)

这是一个完整的(快速和脏)工作示例,通过StringRerader模拟文件内容:

private static final String FILE_CONTENT =
        "1000,Adams,Olivi,80,78,61,91,20,18,18,20,19,20,19,20,20,19,20,BUS,3.1\n" +
        "1001,Smith,Kimberly,70,68,53,72,20,20,18,20,18,18,20,19,20,19,18,IMD,3.5\n" +
        "1002,Fenandez,Aaron,76,67,72,99,18,20,20,19,19,19,19,19,19,19,20,BIO,2.7";

public static void main(String[] args) throws IOException {
    int[][] studentScores = new int[3][15];
    float[] studentGPA = new float[3];
    BufferedReader bufferedReader = new BufferedReader(new StringReader(FILE_CONTENT));
    int count = 0;
    for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
        String[] values = line.split(",");
        for (int idx = 0; idx < 15; idx++) studentScores[count][idx] = Integer.parseInt(values[idx + 3]);
        studentGPA[count] = Float.parseFloat(values[19]);
        System.out.println(studentGPA[count] + " : " + Arrays.toString(studentScores[count]));
        count++;
    }
}

输出:

3.1 : [80, 78, 61, 91, 20, 18, 18, 20, 19, 20, 19, 20, 20, 19, 20]
3.5 : [70, 68, 53, 72, 20, 20, 18, 20, 18, 18, 20, 19, 20, 19, 18]
2.7 : [76, 67, 72, 99, 18, 20, 20, 19, 19, 19, 19, 19, 19, 19, 20]