从Java目录中读取文件的奇怪错误

时间:2012-05-29 11:11:49

标签: java filesystems

编辑: children是一个目录数组。此代码循环通过此数组,以便进入每个目录并加载到数组 webs 列出的所有文件。然后,对于每个文件,readFile函数应该读取文件。

我的代码是:

for (File cat: children) {
    File[] webs = cat.listFiles();
    System.out.println("  Indexing category: " + cat.getName());
    for (File f: webs) {                    
        Web w = readFile(f);                
       // Do things with w  
    }   
}   

我收到此错误:

org.htmlparser.util.ParserException: Error in opening a connection to 209800.webtrec
209801.webtrec
     ...     
422064.webtrec
422071.webtrec
422087.webtrec
422089.webtrec
422112.webtrec
422125.webtrec
422127.webtrec
;
java.io.IOException: File Name Too Long
    at java.io.UnixFileSystem.canonicalize0(Native Method)
at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:172)
at java.io.File.getCanonicalPath(File.java:576)
at org.htmlparser.http.ConnectionManager.openConnection(ConnectionManager.java:848)
at org.htmlparser.Parser.setResource(Parser.java:398)
at org.htmlparser.Parser.<init>(Parser.java:317)
at org.htmlparser.Parser.<init>(Parser.java:331)
at IndexGenerator.IndexGenerator.readFile(IndexGenerator.java:156)
at IndexGenerator.IndexGenerator.main(IndexGenerator.java:101)

这很奇怪,因为我在该目录中看不到任何这些文件。

谢谢!

EDIT2:这是readFile函数。它将文件的内容加载到字符串中并解析它。实际上,文件是html文件。

private static Web readFile(File file) {
    try {           
        FileInputStream fin = new FileInputStream(file);
        FileChannel fch = fin.getChannel();

        // map the contents of the file into ByteBuffer
        ByteBuffer byteBuff = fch.map(FileChannel.MapMode.READ_ONLY, 
                0, fch.size());

        // convert ByteBuffer to CharBuffer
        // CharBuffer chBuff = Charset.defaultCharset().decode(byteBuff);
        CharBuffer chBuff = Charset.forName("UTF-8").decode(byteBuff);
        String f = chBuff.toString();

        // Close imputstream. By doing this you close the channel associated to it
        fin.close();            

        Parser parser = new Parser(f);          
        Visitor visit = new Visitor();
        parser.visitAllNodesWith((NodeVisitor)visit);           
        return new Web(visit.getCat(), visit.getBody(), visit.getTitle());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

好的,最后我得到了解决方案。

这是一个非常愚蠢的错误。我在该目录中有一个文件,其中包含我在上一个任务中删除的所有空html文件的名称。所以,我试图解析它,然后解析器将它解释为URL而不是htmlfile(因为没有标签和很多点......)。我无法轻易找到该文件,因为我在该文件夹中有数百万个文件。