【问题标题】:How to read a text file into an array list of objects in Java如何将文本文件读入Java中的对象数组列表
【发布时间】:2022-09-26 15:52:36
【问题描述】:

所以我目前正在做一个项目,我遇到了几个问题。该项目涉及使用 2 个类,Subject 和 TestSubject。基本上,我需要我的程序(在 TestSubject 类中)从文本文件中读取详细信息(主题代码和主题名称)并使用此信息创建主题对象,然后将它们添加到数组列表中。文本文件如下所示(没有空行):

ITC105:通信和信息管理

ITC106:编程原理

ITC114:数据库系统简介

ITC161:计算机系统

ITC204:人机交互

ITC205:专业编程实践

第一部分是主题代码,即 ITC105,第二部分是名称(通信和信息管理)

我已经创建了主题对象,其代码和名称作为带有 getter 和 setter 的字符串以允许访问(在主题类中):

private static String subjectCode;
private static String subjectName;

public Subject(String newSubjectCode, String newSubjectName) {
    newSubjectCode = subjectCode;
    newSubjectName = subjectName;
}

public String getSubjectCode() {
    return subjectCode;
}
public String getSubjectName() {
    return subjectName;
}

public void setSubjectCode(String newSubjectCode) {
    subjectCode= newSubjectCode; 
}

public void setSubjectName(String newSubjectName) {
    subjectName = newSubjectName; 
}

到目前为止,我用于读取文件和创建数组列表的代码是:

public class TestSubject {
   @SuppressWarnings({ \"null\", \"resource\" })
   public static void main(String[] args) throws IOException {
    
    File subjectFile = new File (\"A:\\\\Assessment 3 Task 1\\\\src\\\\subjects.txt\");
    Scanner scanFile = new Scanner(subjectFile);
    
    System.out.println(\"The current subjects are as follows: \");
    System.out.println(\" \");

    while (scanFile.hasNextLine()) {
        System.out.println(scanFile.nextLine());
    }
    
    //This array will store the list of subject objects. 
    ArrayList <Object> subjectList = new ArrayList <>();
    
    //Subjects split into code and name and added to a new subject object.
    String [] token = new String[3];
    
    while (scanFile.hasNextLine()) {
        token = scanFile.nextLine().split(\": \");
        String code = token [0] + \": \";
        String name = token [1];
        
        Subject addSubjects = new Subject (code, name);
        
        //Each subject is then added to the subject list array list. 
        subjectList.add(addSubjects);
    }
    
    //Check if the array list is being filled by printing it to the console.
    System.out.println(subjectList.toString());

此代码不起作用,数组列表只是打印为空白。我已经尝试过几种方法,包括缓冲阅读器,但到目前为止我无法让它工作。下一段代码允许用户输入主题代码和名称,然后也将其添加到数组列表中。那段代码完美运行,我只是停留在上面的部分。任何关于如何修复它以使其工作的建议都会很棒。

还有一件小事:

 File subjectFile = new File (\"A:\\\\Assessment 3 Task 1\\\\src\\\\subjects.txt\"); //this file path
 Scanner scanFile = new Scanner(subjectFile);

我想知道如何更改文件路径,以便在移动文件夹或在另一台计算机上打开文件时它仍然可以工作。 .txt 文件与 java 文件位于源文件夹中。我努力了:

 File subjectFile = new File (\"subjects.txt\");

但这不起作用,只会引发错误。

  • 关于“小事”:您可以在此处使用一些“运行时定义”值(例如 String[] args(Main 方法的)、System.getProperty(..)、properties.load+get...)。 .或者您“只是打包”(即复制)文本文件(在与您的类相同的(类路径)文件夹结构中),您可以参考classpath://.../subjects.txt..

标签: java arraylist text-files logic-error


【解决方案1】:

那是因为你已经通读了文件

while (scanFile.hasNextLine()) {
    System.out.println(scanFile.nextLine());
}

内容已尽。所以当你这样做时

while (scanFile.hasNextLine()) {
    token = scanFile.nextLine().split(": ");

没有数据了。

删除第一个循环或重新打开文件。

或者正如@UsagiMiyamoto 提到的

或者将行读入一个字符串变量,打印它,然后拆分它......全部在一个循环中。

【讨论】:

  • 或者将行读入一个字符串变量,打印它,然后拆分它......全部在一个循环中。
  • @UsagiMiyamoto 谢谢我已经更新了我的答案
【解决方案2】:

您的调试控制台对异常说了什么?

您的代码在我的编辑器中运行良好。

code result

如果你想通过相对路径读取文件,你应该在下面编写代码

之前->

新文件(“A:\Assessment 3 Task 1\src\subjects.txt”);

之后->

new File (".\\subjects.txt");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多