2009-09-20 94 views
-3
private void traverse(String dir, int ctr) throws IOException 

{ 
    // get current file and name        

    File myFile = new File(dir); 
    System.out.println("dir path: " + myFile.getAbsolutePath()); // correct path 
    System.out.println("exists? : " + myFile.exists()); // returns false 
    String name = myFile.getName(); 
    System.out.println(dir + " is dir? " + myFile.isDirectory()); 
    if (name.equals("tree.txt")) 
     return; 

    // print tabs and name 
    for (int i = 0; i < ctr; ++i) 
     bw2.write("\t"); 
    bw2.write(name); 
    bw2.newLine(); 


    if (myFile.isFile() && name.charAt(0) != '.') 

    { 
     File f = new File(dir + "." + name); 
     int version = 1; // if doesn't exist then version is 1 

     if (f.exists()) 
     { 
      FileInputStream fis = new FileInputStream(f); 
      InputStreamReader isr = new InputStreamReader(fis); 
      BufferedReader br = new BufferedReader(isr); 

      version = Integer.parseInt(br.readLine()); // get version 

      br.close(); 
     } 

     fos1 = new FileOutputStream(f); 

     osw1 = new OutputStreamWriter(fos1); 

     bw1 = new BufferedWriter(osw1); 

     bw1.write(version); // write version 

     bw1.close(); 

    } 

    else if (myFile.isDirectory()) 

    { 
     String dirContents[] = myFile.list(); 

     for (String content : dirContents) 

     { 
      traverse(dir + content + '/', ctr + 1); 
     } 

    } 
} // end traverse 

輸出:java語言。它說我的文件不存在,但它確實

[email protected]:~/Desktop/connection$ java Server 
dir path: /home/kedy/Desktop/connection/test.txt 
exists? : false 
test.txt/ is dir? false 
dir path: /home/kedy/Desktop/connection/tree.txt 
exists? : false 
tree.txt/ is dir? false 
dir path: /home/kedy/Desktop/connection/folder 2 
exists? : false 
folder 2/ is dir? false 
dir path: /home/kedy/Desktop/connection/test2.txt 
exists? : false 
test2.txt/ is dir? false 
dir path: /home/kedy/Desktop/connection/folder 
exists? : false 
folder/ is dir? false 
+1

你能否告訴我們,當你執行它的輸出,還有少數的println在那裏。 – dave 2009-09-20 05:53:54

+0

它可能看起來很凌亂cuz我不​​知道如何在這些評論框中發佈代碼 – user176121 2009-09-20 06:07:57

+0

kedy @ Laptop:〜/ Desktop/connection $ java服務器 dir路徑:/home/kedy/Desktop/connection/tree.txt 樹.txt /是否是dir? false dir path:/ home/kedy/Desktop/connection/folder 2 folder 2/is dir? false dir path:/home/kedy/Desktop/connection/test2.txt test2.txt/is dir? false dir path:/ home/kedy/Desktop/connection/folder folder/is dir?假 – user176121 2009-09-20 06:08:53

回答

0

我最終可能會加入到這個答案,但是,第一,你可能要檢查的權限,以查看是否應用程序具有讀權限。

哪個版本的Java,哪個OS?

如果將文件移動到所有用戶都使用的公共目錄,那麼可以找到該文件嗎?

您可能希望將路徑作爲參數,因此您可以在不重新編譯應用程序的情況下輕鬆更改該應用程序,從而進行測試。

+0

java jdk 1.6,ubuntu。我從客戶端代碼中複製了大部分代碼(客戶端工作)。是的,應用程序具有讀取權限。 – user176121 2009-09-20 05:50:31

2

看着輸出,我會冒險猜測,確實沒有文件"test.txt/"。但是,可能有一個文件"text.txt"

2

變化

traverse(dir + content + '/', ctr + 1); 

traverse(dir + '/' + content, ctr + 1); 
+0

這顯然是一個錯誤,但這不會改變輸出,因爲程序不應該到達這裏,因爲似乎沒有任何一個目錄 – user176121 2009-09-20 13:20:36

相關問題