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

所以我有一个文本文件,其中包含有关课程的一些信息,例如课程 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.0


MAT210 

Applied Linear Algebra 

description here 

3.0 


...and so on

感谢您的帮助,抱歉,如果我解释得不好,这里是第一个问题


编辑:是的,我已经定义了 Course 类,其中包含所有 getter 和 setter 以及适当的字段。


莫回无
浏览 64回答 2
2回答

浮云间

我假设您已经有一个 POJO 课程,如下所示:class Course {&nbsp; &nbsp; private String crn;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private String description;&nbsp; &nbsp; private String credit;&nbsp; &nbsp; //general getters and setters&nbsp;}然后以下示例代码展示了如何使用BufferedReader读取文本文件并将内容存储到 Collection 中List<Course>。List<Course> courses = new ArrayList<>();try (BufferedReader br = Files.newBufferedReader(Paths.get("testC.txt"))) {&nbsp; &nbsp; String line;&nbsp; &nbsp; Course course = new Course();&nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; if (!line.trim().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (course.getCrn() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course.setCrn(line.trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (course.getName() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course.setName(line.trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (course.getDescription() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course.setDescription(line.trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course.setCredit(line.trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; courses.add(course);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course = new Course();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }} catch (IOException e) {&nbsp; &nbsp; //TODO exception handling}

UYOU

也许你可以尝试下面的方法。String sb;&nbsp; &nbsp; try(BufferedReader br = new BufferedReader(new FileReader("kmh.txt"))) {&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while((sb = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// System.out.println(sb); //just prints out all lines identical to text file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!sb.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String courseCRN = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String courseFullName = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String courseDescription = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String courseCredits = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count == 0)&nbsp; courseCRN = sb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count == 1)&nbsp; courseFullName = sb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count == 2)&nbsp; courseDescription = sb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count == 3)&nbsp; courseCredits = sb;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Save your course data in map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count == 4) count = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java