java - 如何使用 Java 中的 BufferedReader 从文件中读取整数?

标签 java file io integer bufferedreader

我正在 Java 中使用 BufferedReader,希望在读取整数时得到一些指导。

总而言之,输入文件的每一行将代表无向图中的一条边。它将包含两个整数,即边的端点,后跟一个实数,即边的权重。最后一行将包含 -1,表示输入结束。

我创建了一个 BufferedReader 对象并初始化了一个整数变量

文件格式如下:

 0   1    5.0
 1   2    5.0
 2   3    5.0
...
 5  10    6.0
 5  11    4.0
17  11    4.0
-1
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
        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.");
        }
    } 

这是我迄今为止所尝试的,但我想知道如何获取每一行代码,并让第一个数字为“开始”变量,第二个数字为“结束”变量,第三个数字为“开始”变量是“体重”变量​​?我在网上看到了一些创建数组的解决方案,但由于我的文件格式,我有些困惑。我可以帮助澄清有关的任何细节

最佳答案

我首先检查是否可以读取该文件(您可以使用 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);
            }
        }
    }
}

关于java - 如何使用 Java 中的 BufferedReader 从文件中读取整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55368568/

相关文章:

io - 如何在 mio 中复制 POLLPRI 的轮询行为?

java - Kafka : How to consume, 操作、确认(手动)并将结果发送到新主题

java - 我似乎无法让这个继承发挥作用

java - 计算 Java 字符串中的重复单词。奇数终端显示

Java Spring - 编写 Excel 文件并转换为 PDF

c++ - (c/c++) 试图强制 EOF 从父进程发送输入到子进程

java - 当我尝试写入文件时,为什么 Eclipse IDE 会给出终止错误?

java - 如何使用 Dagger 2 将服务注入(inject) JavaFX Controller

c - 在C中,如何根据文件内容退出循环

python - 将 xml 节点和子节点复制到新的 xml 文件