为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用缓冲读取器保存文本文件中的行

如何使用缓冲读取器保存文本文件中的行

莫回无 2023-12-13 14:28:03
所以我有一个文本文件,其中包含有关课程的一些信息,例如课程 CRN、课程全名、课程描述、课程学分。文件中还有很多类似的课程,每 4 行以换行符分隔。我需要将每一行保存到一个字符串中,以便稍后传递到构造函数中,以便我可以将它们存储在哈希图中。此外,在每一个新行之后,它都会知道新的课程信息已经开始。但我不太熟悉缓冲阅读器来做到这一点。到目前为止,我只拥有它,它可以读取和输出每一行,但我需要保存每一行(CRN 到 CRN、名称到名称等),这是我到目前为止所拥有的:有问题的方法:public void addCourses() {        String sb;        try(BufferedReader br = new BufferedReader(new FileReader("testC.txt"))) {            while((sb = br.readLine()) != null) {                  System.out.println(sb); //just prints out all lines identical to text file                //Do stuff here?            }        } catch(Exception e) {            e.printStackTrace();        }    }文本文件看起来像这样:MAT205 Discrete Mathematics description here 4.0MAT210 Applied Linear Algebra description here 3.0 ...and so on感谢您的帮助,抱歉,如果我解释得不好,这里是第一个问题编辑:是的,我已经定义了 Course 类,其中包含所有 getter 和 setter 以及适当的字段。
查看完整描述

2 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

我假设您已经有一个 POJO 课程,如下所示:


class Course {

    private String crn;

    private String name;

    private String description;

    private String credit;


    //general getters and setters 

}

然后以下示例代码展示了如何使用BufferedReader读取文本文件并将内容存储到 Collection 中List<Course>。


List<Course> courses = new ArrayList<>();

try (BufferedReader br = Files.newBufferedReader(Paths.get("testC.txt"))) {

    String line;

    Course course = new Course();

    while ((line = br.readLine()) != null) {

        if (!line.trim().isEmpty()) {

            if (course.getCrn() == null) {

                course.setCrn(line.trim());

            } else if (course.getName() == null) {

                course.setName(line.trim());

            } else if (course.getDescription() == null) {

                course.setDescription(line.trim());

            } else {

                course.setCredit(line.trim());

                courses.add(course);

            }

        } else {

            course = new Course();

        }

    }

} catch (IOException e) {

    //TODO exception handling

}


查看完整回答
反对 回复 2023-12-13
?
UYOU

TA贡献1878条经验 获得超4个赞

也许你可以尝试下面的方法。


String sb;

    try(BufferedReader br = new BufferedReader(new FileReader("kmh.txt"))) {

        int count = 0;

        while((sb = br.readLine()) != null) {

           // System.out.println(sb); //just prints out all lines identical to text file

            if(!sb.isEmpty()){


                String courseCRN = null;

                String courseFullName = null;

                String courseDescription = null;

                String courseCredits = null;

                if(count == 0)  courseCRN = sb;

                if(count == 1)  courseFullName = sb;

                if(count == 2)  courseDescription = sb;

                if(count == 3)  courseCredits = sb;

                count++;


               //Save your course data in map

            }


            if(count == 4) count = 0;


        }

    } catch(Exception e) {

        e.printStackTrace();

    }


查看完整回答
反对 回复 2023-12-13
  • 2 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信