亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何使用Java中的BufferedReader從文件中讀取整數?

如何使用Java中的BufferedReader從文件中讀取整數?

慕標5832272 2022-09-07 16:04:26
我正在使用Java中的BufferedReader,并希望在讀取整數時獲得一些指導??偠灾?,輸入文件的每一行將表示無向圖中的一條邊。它將包含兩個整數,即邊緣的端點,后跟一個實數,即邊緣的權重。最后一行將包含一個 -1,表示輸入的結束。我創建了一個BufferedReader對象并初始化了一個整數變量和該文件的格式如下: 0   1    5.0 1   2    5.0 2   3    5.0... 5  10    6.0 5  11    4.017  11    4.0-1public static void processFile(String inputFilePath) throws IOException {        //Check to see if file input is valid        if (inputFilePath == null || inputFilePath.trim().length() == 0) {            throw new IllegalArgumentException("Error reading file.");        }        //Initialize required variables for processing the file        int num = 0;        int count = 0;        try {            //We are reading from the file, so we can use FileReader and InputStreamReader.            BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath));            //Read numbers from the line            while ((num = fileReader.read()) != -1) { //Stop reading file when -1 is reached                //First input is the start                //Second input is the end                //Third input is the weight            }        } catch (IOException e) {            throw new IOException("Error processing the file.");        }    } 這是我到目前為止所嘗試的,但我想知道我如何獲取每行代碼,并使第一個數字是“開始”變量,第二個數字是“結束”變量,第三個數字是“權重”變量?我在網上看到了一些創建數組的解決方案,但由于我的文件格式,我有點困惑。我可以幫助澄清有關以下方面的任何細節:
查看完整描述

3 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

我會首先檢查我是否可以讀取該文件(您可以使用File.canRead()來執行此操作)。接下來,我將編譯一個具有三個分組操作的正則表達式。然后我會使用BufferedReader.readLine()來讀取文本行;read() 調用返回單個字符。然后它只剩下解析匹配的行。而且我認為吞下原始異常只是為了重新刪除它(事實上,你以當前的方式丟失了所有堆棧跟蹤信息)。我看不出有什么意義。把這些放在一起,

public static void processFile(String inputFilePath) throws IOException {

    File f = new File(inputFilePath);

    if (!f.canRead()) {

        throw new IllegalArgumentException("Error reading file.");

    }


    // Initialize required variables for processing the file

    try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {

        Pattern p = Pattern.compile("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d.+)$");

        String line;

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

            Matcher m = p.matcher(line);

            if (m.matches()) {

                int start = Integer.parseInt(m.group(1));

                int end = Integer.parseInt(m.group(2));

                double weight = Double.parseDouble(m.group(3));

                System.out.printf("start=%d, end=%d, weight=%.2f%n", start, end, weight);

            }

        }

    }

}


查看完整回答
反對 回復 2022-09-07
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

切換到readLine并使用掃描儀:


public static void processFile(String inputFilePath) throws IOException {

    // Check to see if file input is valid

    if (inputFilePath == null || inputFilePath.trim()

                                              .length() == 0) {

        throw new IllegalArgumentException("Error reading file.");

    }


    // Initialize required variables for processing the file

    String line;

    int count = 0;


    // We are reading from the file, so we can use FileReader and InputStreamReader.

    try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {


        // Read numbers from the line

        while ((line = fileReader.readLine()) != null) { // Stop reading file when -1 is reached

            Scanner scanner = new Scanner(line);


            // First input is the start

            int start = scanner.nextInt();


            if (start == -1) {

                break;

            }


            // Second input is the end

            int end = scanner.nextInt();


            // Third input is the weight

            double weight = scanner.nextDouble();


            // do stuff

        }

    } catch (IOException e) {

        throw new IOException("Error processing the file.");

    }

}


查看完整回答
反對 回復 2022-09-07
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

而不是使用,你可以只是使用然后使用分裂與你的分隔符是三個空格,我想?readreadLine


        try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {

            String line;

            while(!(line = fileReader.readLine()).equals("-1")) {

                String[] edge = line.split("   ");

                int start = Integer.parseInt(edge[0]);

                int end = Integer.parseInt(edge[1]);

                double weight = Double.parseDouble(edge[2]);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }


查看完整回答
反對 回復 2022-09-07
  • 3 回答
  • 0 關注
  • 286 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號