2010-07-26 33 views
5

我正在處理該程序以獲取目錄中的所有文件。出於某種原因,我得到了第16行的NullPointerException。我不知道爲什麼,因爲這是一個模板,似乎與我們的老師在課堂上工作。謝謝。用Java獲取目錄中的所有文件的程序

import java.util.*; 
import java.io.*; 

public class FindDirectories { 
    public static void main(String[] args) { 
     if (args.length == 0) { 
      args = new String[] { ".." }; 
     } 

     List<String> nextDir = new ArrayList<String>(); 
     nextDir.add(args[0]); // either the one file, or the directory 
     try { 
      while(nextDir.size() > 0) {  // size() is num of elements in List 
       File pathName = new File(nextDir.get(0)); // gets the element at the index of the List 
       String[] fileNames = pathName.list(); // lists all files in the directory 
       for(int i = 0; i < fileNames.length; i++) { 
        File f = new File(pathName.getPath(), fileNames[i]); // getPath converts abstract path to path in String, 
                    // constructor creates new File object with fileName name 
        if (f.isDirectory()) { 
        System.out.println(f.getCanonicalPath()); 
        nextDir.add(f.getPath()); 
        } 
        else { 
         System.out.println(f); 
        } 
       } 
       nextDir.remove(0); 
      } 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     }  
    } 
} 

回答

9

退房的Javadoc for File.list()。具體而言:

如果此抽象路徑名不表示目錄或發生I/O錯誤,則返回null。

在您的代碼pathName.list();必須返回null所以pathName並不代表一個有效的目錄,或IO出錯試圖從該目錄中的文件列表。

+0

我如何檢查呢?我只是說pathName.list()!= null? – Crystal 2010-07-30 06:21:58

+0

是@Crystal,你應該測試一下。然後決定在發生這種情況時應採取的措施:報告錯誤,提示用戶輸入正確的目錄等。 – krock 2010-07-30 10:19:14

0

如果您在第16行中收到NullPointerException,則這意味着fileNames爲空,因此fileNames.length無效。看看the javadoc for File.list,如果pathName不是目錄,或者發生異常,您將看到pathName.list()可以爲空。所以你只需要在試圖使用它之前檢查fileNames是否爲空。

0
import java.io.File; 
import java.util.ArrayList; 
import java.util.LinkedList; 


public class FileEnumerator { 

    /** 
    * @param args 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException { 

     // Prepare the List of files 
     String path = "C:/"; 
     ArrayList<String> Files = new ArrayList<String>(); 
     LinkedList<String> Dir = new LinkedList<String>(); 
     File f = new File(path); 
     Dir.add(f.getAbsolutePath()); 
     while(!Dir.isEmpty()) 
     { 
      f = new File(Dir.pop()); 
      if(f.isFile()) 
      { 
       Files.add(f.getAbsolutePath()); 
      } 
      else 
      { 
       String arr[] = f.list(); 
       try 
       { 
       for(int i = 0;i<arr.length;i++) 
       { 
        Dir.add(f.getAbsolutePath()+"/"+arr[i]); 
       } 
       } 
       catch(NullPointerException exp) 
       { 
        Dir.remove(f.getAbsoluteFile()); 
       } 
      } 
     } 


       //Print the files 
     for(int i = 0;i<Files.size();i++) 
     { 
      System.out.println(Files.get(i)); 
     } 
    } 

} 

我認爲這段代碼應該可以正常工作。儘管我已經在Windows上測試過它。但其他操作系統最多隻需要很小的更改。

+0

「我認爲這段代碼應該可以正常工作,儘管我已經在Windows上進行了測試,但其他操作系統最多隻需要很小的修改。鑑於良好的Java代碼應該在其他操作系統上運行,並且**沒有**更改,您的答案中的代碼是*不是一個好例子。至少它應該使用System.getProperty(「file.separator」)或接受路徑/名稱參數而不是'/'的File構造函數。然後,我們可能會使用'String'對象來表示'File's。假定一個'C:'驅動器而不是接受一個命令行參數。 (...) – 2011-04-23 10:48:41

4

使用波紋管段從所有子目錄得到所有的文件:

import java.io.File; 

/** 
* 
* @author santoshk 
*/ 
public class ListFiles { 

    File mainFolder = new File("F:\\personal"); 
    public static void main(String[] args) 
    { 
     ListFiles lf = new ListFiles(); 
     lf.getFiles(lf.mainFolder); 
    } 
    public void getFiles(File f){ 
     File files[]; 
     if(f.isFile()) 
      System.out.println(f.getAbsolutePath()); 
     else{ 
      files = f.listFiles(); 
      for (int i = 0; i < files.length; i++) { 
       getFiles(files[i]); 
      } 
     } 
    } 
} 
0
import java.io.*; 

public class filedir 
{ 
    public static void main(String[] args) 
    { 
     try{ 
      Files f = new File("C:\\");//the path required 
      String a[]; 
      a=f.list(); 
      for (int i = 0; i <a.length; i++) { 
       System.out.println(a[i]); 
       } 
     } catch(Exception e) { 
      System.err.println(e); 
     } 
    } 
} 
+1

我希望你用這個小代碼得到想法...對不起,如果它不是問題的答案。 – anjuna 2013-01-23 19:20:03

相關問題