我正在使用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.");
}
}
这是我到目前为止所尝试的,但我想知道我如何获取每行代码,并使第一个数字是“开始”变量,第二个数字是“结束”变量,第三个数字是“权重”变量?我在网上看到了一些创建数组的解决方案,但由于我的文件格式,我有点困惑。我可以帮助澄清有关以下方面的任何细节:
狐的传说
冉冉说
心有法竹
相关分类