public class parseFiles
{
public static void main(String... aArgs) throws FileNotFoundException
{
File startingDirectory= new File("CGT");
List<File> files = FileListing2.getFileListing(startingDirectory);
for(File file : files)
{
System.out.println(file);
}
}
<other methods to supply the file listings, etc.>
}
這就是我現在的位置:這很好,並且具有全路徑輸出到控制檯的文件列表沒有任何問題。現在,我想要獲取輸出中列出的每個文件,並逐行讀取它們。如何將列表傳遞給Java輸入流?
BufferedReader br = new BufferedReader(new FileReader(file));
String inputLine;
String desc = "";
String docNo = "";
//
while ((inputLine = br.readLine()) != null)
{
int testVal=0;
String delim = ",";
int stringMax = inputLine.length();
if(inputLine.startsWith("Description"))
{desc = inputLine.substring(13,inputLine.length());}
else
if(inputLine.startsWith("Reference Number"))
{docNo = inputLine.substring(20,inputLine.length());}
String outputString = desc+delim+docNo;
//
<write series of output strings to flat file>
//
}
while ((inputLine = br.readLine()) != null)
保持與以下錯誤踢回:
FileListing2.java:22: error: unreported exception FileNotFoundException; must be
caught or declared to be thrown
List<File> files = FileListing2.getFileListing(startingDirectory);
^
FileListing2.java:30: error: unreported exception FileNotFoundException; must be
caught or declared to be thrown
BufferedReader br = new BufferedReader(new FileReader(file));
^
FileListing2.java:43: error: unreported exception IOException; must be caught or
declared to be thrown
while ((inputLine = br.readLine()) != null)
^
3 errors
@ DavidWallace-就像我說的,我從我所知道的東西中複製了代碼。我是一個java I/O新手,我似乎回想起如果我想逐行解析一個文件,我需要一個數據流而不是File。如果這是一個錯誤的假設,讓我上學。 :) – dwwilson66 2012-04-24 12:20:39
你在說什麼?這是你發佈的編譯器錯誤。這不是一個例外!只需聽取錯誤消息並在您的方法上聲明異常。 – 2012-04-24 12:43:39
這不是一個例外,而是一個編譯錯誤。您需要捕獲或拋出異常。如果你不明白這一點,請查看[例外教程](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)。 – 2012-04-24 14:13:24