将文本文件导入数组以创建对象

Import Text File into an Array to create an object

我正在尝试使用一个文本文件将各种参数输入到一个数组中,然后使用该数组创建一个对象。文本文件中的每个留置权都有字符串,每个对象用逗号分隔,单个对象用单独的行分隔。

我似乎无法弄清楚我做错了什么,当我尝试调用加载文件的方法时,我一直收到此错误: "Exception in thread "main" java.io.FileNotFoundException: studentData.txt (系统找不到指定的文件)"

这是我的方法:

    public void loadStudent() throws FileNotFoundException{

    File inputFile = new File("studentData.txt");
    Scanner input = new Scanner(inputFile);
    try{
    while(input.hasNext()){
        String info = input.nextLine();
        String elements[] = info.split(" , ");
        String fName = elements[0];
        String lName = elements[1];
        String phone = elements[2];
        String address = elements[3];
        double gpa = Double.parseDouble(elements[4]);
        String major = elements[5];
        Student student = new Student(fName, lName, phone, address, 
                                          gpa, major);
        addStudent(student);
        input.nextLine();
        count++;
    }
    input.close();

    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
}

您收到的 FileNotFoundException 表明其名称的含义:JVM 无法在该位置找到该文件。由于您指定的路径是相对的(不是绝对的),因此它很可能不是您认为的位置(相对于您所在的工作目录)。要解决此问题,您可以: a) 将文件路径更新为相对于您的应用所在位置的正确路径 运行 或 b) 指定输入文件的绝对路径

好的,你有两个选择...

选项 #01...

您可以将文件包含在项目本身中,方法是将文件放在 src 目录中。这会将文件嵌入到您的程序中,使其成为只读

(是的,我知道我在使用 Netbeans,这个概念在 Eclipse 中是相同的)

如您所见,studentData.txtmyawesomeproject 中,旁边是我的 Main class。在 src 中放置文件的位置很重要,因此请注意,如果文件不在同一个包中,则您需要提供相对或完全限定的路径。

接下来我们使用Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt"))打开文件,例如...

package myawesomeproject;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        try {
            loadStudent();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void loadStudent() throws FileNotFoundException {
        try (Scanner input = new Scanner(getClass().getResourceAsStream("studentData.txt"))) {
            while (input.hasNext()) {
                String info = input.nextLine();
                System.out.println(info);
                String elements[] = info.split(" , ");
                String fName = elements[0];
                String lName = elements[1];
                String phone = elements[2];
                String address = elements[3];
                double gpa = Double.parseDouble(elements[4]);
                String major = elements[5];

            }
        }
    }
}

在我的示例中,打印...

B1 , B2 , B3 , B4 , 0 , B6

(这是我文件的内容)

选项 #02...

您将文件放在“工作”目录中,这是执行程序的上下文,通常是项目目录(包含 src 目录的目录)

那你可以用Scanner input = new Scanner(new File("studentData.txt"))打开,比如...

package myawesomeproject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        try {
            loadStudent();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void loadStudent() throws FileNotFoundException {
        try (Scanner input = new Scanner(new File("studentData.txt"))) {
            while (input.hasNext()) {
                String info = input.nextLine();
                System.out.println(info);
                String elements[] = info.split(" , ");
                String fName = elements[0];
                String lName = elements[1];
                String phone = elements[2];
                String address = elements[3];
                double gpa = Double.parseDouble(elements[4]);
                String major = elements[5];

            }
        }
    }
}

输出与上面相同的内容,因为我使用了相同的文件,只是为了示例移动了它

这假设您没有更改“工作”目录。如果您可以使用 System.out.println(System.getProperty("user.dir")); 测试工作目录的位置,那么您可以将文件移动到此位置