Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用缓冲读取器保存文本文件中的行_Java_Text Files_Bufferedreader - Fatal编程技术网

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

Java 如何使用缓冲读取器保存文本文件中的行,java,text-files,bufferedreader,Java,Text Files,Bufferedreader,所以我有一个文本文件,里面有一些关于课程的信息,比如课程CRN,课程全名,课程描述,课程学分。 文件中还有更多类似的课程,以4行为一行,以新行分隔。我需要将每一行保存到一个字符串中,以便稍后传递到构造函数中,以便将它们存储在hashmap中。同样,在每一行之后,它将知道一个新的课程信息已经开始。但我不太熟悉缓冲读取器来实现这一点。到目前为止,我只有它可以读取和输出每一行,但我需要保存每一行(CRN到CRN,名称到名称,等等) 以下是我到目前为止的情况: 有关方法: public void add

所以我有一个文本文件,里面有一些关于课程的信息,比如课程CRN,课程全名,课程描述,课程学分。 文件中还有更多类似的课程,以4行为一行,以新行分隔。我需要将每一行保存到一个字符串中,以便稍后传递到构造函数中,以便将它们存储在hashmap中。同样,在每一行之后,它将知道一个新的课程信息已经开始。但我不太熟悉缓冲读取器来实现这一点。到目前为止,我只有它可以读取和输出每一行,但我需要保存每一行(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
谢谢你的帮助对不起,如果我解释的东西不好,第一个问题在这里


编辑:是的,我已经定义了一个包含所有getter和setter以及相应字段的课程类。

也许你可以尝试以下方法

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

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

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

    //general getters and setters 
}
下面的示例代码显示了如何使用
BufferedReader
读取文本文件并将内容存储到集合
列表中

List courses=new ArrayList();
try(BufferedReader br=Files.newBufferedReader(path.get(“testC.txt”)){
弦线;
课程=新课程();
而((line=br.readLine())!=null){
如果(!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());
}否则{
course.setCredit(line.trim());
课程。添加(课程);
}
}否则{
课程=新课程();
}
}
}捕获(IOE异常){
//TODO异常处理
}

非常感谢您的支持。我不知道该如何处理循环本身,但这给了我一个非常清晰的视图。我创建了一个类似的循环,它利用了HashMap,我还添加了一个这样的循环,它将把积分转换成一个double(因为在给定的txt文件中,所有的积分都是(number).0),并且一切都像一个符咒一样工作。
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
}