0
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.>
}           

This is where I am right now: This works great, and a list of files with full paths outputs to the console without any problems. Now, I want to take each of the files listed in that output, and read them line by line.

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) keeps kicking back with the following error:

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
4

2 回答 2

4

Instead of

DataInputStream in = new DataInputStream(file); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

Write

BufferedReader br = new BufferedReader(new FileReader(file));
于 2012-04-24T12:19:02.370 回答
0

You could try the IOHelper from Java Helper on github. There are methods for getting all files in a directory and reading a file to a string.

于 2012-04-24T12:21:45.783 回答