如何将列表传递给Java输入流?

时间:2012-04-24 12:15:43

标签: java file-io datainputstream

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

2 个答案:

答案 0 :(得分:4)

而不是

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

BufferedReader br = new BufferedReader(new FileReader(file));

答案 1 :(得分:0)

您可以在github上尝试来自IOHelperJava Helper。有一些方法可以将所有文件放在目录中并将文件读取到字符串中。