简体   繁体   中英

How to save lines from a text file using buffered reader

So I have a text file that has some info about courses such as course CRN, course full name, course description, course credits. And there are many more courses like this in the file in lines of 4 separated by a new line. I need to save each line into a string to later pass into a constructor so I can store them in a hashmap. Also after each new line it will know that a new course information has started. But I am not much familiar with the buffered reader to do that. I so far just have it that it can read and output each line but I need to save each line (CRN to CRN, name to name, etc) here is what I have so far:

method in question:

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();
        }
    }

text file looks something like this:

MAT205 
Discrete Mathematics 
description here 
4.0

MAT210 
Applied Linear Algebra 
description here 
3.0 

...and so on

Thanks for any help sorry if I am explaining stuff bad, first question on here

Edit: Yes I already have a defined Course class with all getters and setters and appropriate fields.

Maybe you can try the below approach.

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();
    }

I assumed that you have already a POJO for course which looks like below:

class Course {
    private String crn;
    private String name;
    private String description;
    private String credit;

    //general getters and setters 
}

Then following sample code shows how to use BufferedReader to read text file and store the content into 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM