gpt4 book ai didi

java - 如何通过java读取文件名、定位并打开它

转载 作者:行者123 更新时间:2023-11-30 04:01:24 26 4
gpt4 key购买 nike

我写了一个文件,其中包含 GPS 坐标列表,它只有 2 列经度和纬度,仅此而已。我有第二个文件,其中包含 GPS 坐标文本文件名和 deviceId 列表,例如 - Routes_A.txt 285, 284我要做的就是编写代码来打开文件并读取它,这是我可以做到的;

File textA = new File("C:/Users/Daniel Dold/Desktop/Routes/Bus_Routes.txt");
Scanner scannerA = new Scanner(textA);
while(scannerA.hasNextLine())
{
String line = scannerA.nextLine();
System.out.println(line);
}

我遇到的问题是,当代码打开 Bus_Routes.txt 文件时,我需要能够打开 Routes_A.txt 文件,而不是将其硬编码到我的代码中。

有谁有任何信息可以帮助我吗?

最佳答案

您可以解析从 Bus_Routes.txt 读取的行以提取第二个文件名:

String line = scannerA.nextLine();
String[] parsed = line.split("\\s"); //split at whitespace
String otherFileName = parsed[0]; //other filename was 1st part of line

File dir = //what directory are the files in?
File otherFile = new File(dir, otherFileName);
//now read this file the same way as you read the previous one

关于java - 如何通过java读取文件名、定位并打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com