【发布时间】:2012-04-24 12:15:42
【问题描述】:
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.>
}
这就是我现在所处的位置:这很好用,带有完整路径的文件列表输出到控制台没有任何问题。现在,我想获取该输出中列出的每个文件,并逐行读取它们。
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 新手,我似乎记得如果我想逐行解析文件,我需要一个数据流而不是文件。如果这是一个不正确的假设,请教我。 :)
-
你在说什么?这是您发布的编译器错误。也不例外!只需听错误消息并在您的方法上声明异常即可。
-
这不是异常,而是编译错误。您需要捕获或抛出异常。不明白的请看Exception Tutorial。
标签: java file-io datainputstream