2014-02-20 19 views
1

我有一個寫入的文件,其中包含一個GPS座標列表,它只有2列的經度和緯度,就是這樣。 我有第二個文件,將包含GPS座標文本文件名和deviceId的列表,例如 - Routes_A.txt 285, 284 我要做的是編寫代碼打開文件並閱讀它,我可以做;如何讀取文件名,找到它並通過java打開它

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文件,並且不讓它在我的代碼硬編碼。

有沒有人有任何信息可以幫助我?

+0

您的問題究竟是什麼?分割線?打開一個文件? –

+0

我想打開Bus_Routes.txt文件,讀取第一行,看到它的一個文本文件名,然後打開Routes_A.txt文件,這樣就可以獲得這個信息 – Dan

回答

2

您可以分析從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 
+0

完美,歡呼的幫助 – Dan

相關問題