为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用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 关注
  • 292 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号