2012-05-29 67 views
0

編輯: children是一組目錄。此代碼循環播放此數組以便進入每個目錄並加載到數組網絡列出的所有文件。然後,對於每個文件,readFile函數應該讀取該文件。從Java目錄中讀取文件的奇怪錯誤

我的代碼是:

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; 
} 
+3

什麼是「兒童」?它從何而來 ?什麼是'readFile'?告訴我們它的源代碼。它應該做什麼? –

+3

你能打印拋出異常的文件名嗎? – hage

+1

什麼是您的Java版本?如果你真的沒有太長的命名文件,你可以嘗試更新你的Java,這也可能是一個錯誤。 – bpgergo

回答

0

好的,終於我得到了解決方案。

這是一個非常愚蠢的錯誤。我在該目錄中有一個文件,其中包含我在之前的任務中刪除的所有空白html文件的名稱。所以,我試圖解析它,然後解析器將它解釋爲一個URL而不是htmlfile(因爲沒有標籤和許多點...)。我無法輕鬆找到該文件,因爲我在該文件夾中有數百萬個文件。