Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何读取文件名,找到它并通过java打开它_Java_File Io - Fatal编程技术网

如何读取文件名,找到它并通过java打开它

如何读取文件名,找到它并通过java打开它,java,file-io,Java,File Io,我写了一个包含GPS坐标列表的文件,它只有两列经度和纬度,就是这样。 我还有第二个文件,其中包含GPS坐标文本文件名和设备ID列表,例如-Routes\u a.txt 285284 我要做的是编写代码打开文件并读取它,我可以这样做 File textA = new File("C:/Users/Daniel Dold/Desktop/Routes/Bus_Routes.txt"); Scanner scannerA = new Scanner(textA); while(scannerA.has

我写了一个包含GPS坐标列表的文件,它只有两列经度和纬度,就是这样。 我还有第二个文件,其中包含GPS坐标文本文件名和设备ID列表,例如-
Routes\u a.txt 285284
我要做的是编写代码打开文件并读取它,我可以这样做

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

你到底有什么问题?分线?打开一个文件?我想打开Bus_Routes.txt文件,读取第一行,看到它是一个文本文件名,然后打开Routes_a.txt文件,这样它就可以获取该信息