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


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

而不是

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

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

你可以在github上尝试Java Helper的IOHelper。有一些方法可以获取目录中的所有文件并将文件读取为字符串。

相关内容

  • 没有找到相关文章

最新更新