第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用緩沖讀取器保存文本文件中的行

如何使用緩沖讀取器保存文本文件中的行

莫回無 2023-12-13 14:28:03
所以我有一個文本文件,其中包含有關(guān)課程的一些信息,例如課程 CRN、課程全名、課程描述、課程學分。文件中還有很多類似的課程,每 4 行以換行符分隔。我需要將每一行保存到一個字符串中,以便稍后傳遞到構(gòu)造函數(shù)中,以便我可以將它們存儲在哈希圖中。此外,在每一個新行之后,它都會知道新的課程信息已經(jīng)開始。但我不太熟悉緩沖閱讀器來做到這一點。到目前為止,我只擁有它,它可以讀取和輸出每一行,但我需要保存每一行(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感謝您的幫助,抱歉,如果我解釋得不好,這里是第一個問題編輯:是的,我已經(jīng)定義了 Course 類,其中包含所有 getter 和 setter 以及適當?shù)淖侄巍?
查看完整描述

2 回答

?
浮云間

TA貢獻1829條經(jīng)驗 獲得超4個贊

我假設(shè)您已經(jīng)有一個 POJO 課程,如下所示:


class Course {

    private String crn;

    private String name;

    private String description;

    private String credit;


    //general getters and setters 

}

然后以下示例代碼展示了如何使用BufferedReader讀取文本文件并將內(nèi)容存儲到 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條經(jīng)驗 獲得超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 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號