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