2014-02-17 87 views
2

首先,讓我告訴你我到目前爲止的代碼:添加文件對象文件對象的數組

public void populateArray(){ 

    //NOTE: THIS METHOD SHOULD BE RUN FIRST. Many of the methods in this class rely on a populated file array to function. 

    // This method will populate our file array. 
    // First, we note the directory we want to pull files from. 
    File folder = new File("HR"); 
    File dir = new File(""); 
    File file = new File(""); 

    //Then we fill an array with the list of documents in that directory 
    //This will also include folders. 
    int count = 0; 
    allDocs = folder.listFiles(); 
    int fileCount = 0; 
    int dirCount = 0; 
    System.out.println("Displaying contents of directory..."); 
    while (count < allDocs.length){ 
     System.out.println("Found: " + allDocs[count]); 
     count++; 
    } 
    System.out.println("Sorting directories and files separately..."); 
    count = 0; 
    while (count < allDocs.length){ 
     if (allDocs[count].isDirectory()){ 
      dir = new File(allDocs[count].getPath()); 
      dirs[dirCount] = dir; 
      System.out.println("Document " + allDocs[count].getPath() + " sorted into -Directory- array at position " + dirCount); 
      dirCount++; 
     } 
     else{ 
      file = new File(allDocs[count].getPath()); 
      files[fileCount] = file; 
      System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount); 
      fileCount++; 
     } 
     count++; 
    } 
    return; 
} 

目錄,文件和allDocs是類內聲明的任何方法之外的數組。

allDocs是我用來獲取感興趣的目錄HR中的每個目錄和文件的數組。我用allDocs = folder.listFiles();

我想讓文件成爲數組來存儲目錄HR中的所有文件,並且我希望目錄是將所有目錄存儲在HR中的數組,但我不想存儲任何人力資源總監的內容。

我嘗試使用println下的循環「分別對目錄和文件進行排序」。

的問題是在這裏:

else{ 
      file = new File(allDocs[count].getPath()); 
      files[fileCount] = file; 
      System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount); 
      fileCount++; 
     } 

它在這裏,因爲在allDocs第一元素是文件。當第一個元素是一個目錄時,也會發生此問題。問題是文件[fileCount] =文件上的空指針異常;線,我不明白。 「文件」不爲空,我只是給它分配了一個值。當然文件[fileCount]在這種情況下爲空,但爲什麼如果我給它賦值呢?

文件聲明:

public class Methods 
{ 

    private static File[] files; 
+0

嘗試具體而不是打字太長。 :) – Batty

+0

告訴你我想做什麼,爲什麼我不能做得不夠具體?我可以告訴你的不多。 – Lighthat

+0

我的意思是,你告訴了一點額外:) – Batty

回答

2

你可能更簡化:

List<File> fileList = new ArrayList<>(); 
List<File> dirList = new ArrayList<>(); 
while (count < allDocs.length){ 
    if (allDocs[count].isDirectory()){ 
     dirList.add(allDocs[count]); 
     System.out.println("Document " + allDocs[count].getPath() 
      + " sorted into -Directory- array at position " + dirCount); 
     dirCount++; 
    } 
    else{ 
     fileList.add[allDocs[count]); 
     System.out.println("Document " + allDocs[count].getPath() 
      + " sorted into -File- array at position " + fileCount); 
     fileCount++; 
    } 
    count++; 
} 
dirs = dirist.toArray(new File[dirList.size()]); 
dirCount = dirs.length; 
files = fileList.toArray(new File[fileList.size()]); 
fileCount = files.length; 

使用List<File>代替File[]似乎appropiate,因爲你不預先知道數組的大小,你需要:

File[] files = new File[n]; 

如果打算要填寫一個文件:

files[fileCount] = ... 
3

您可能忘記分配數組 - 像這樣:

java.io.File [] files = new java.io.File[10]; 

如果你只宣佈它是這樣的:

java.io.File [] files; 

files會將nullfiles[fileCount]將c請使用NullPointerException

正如在評論中提到,您可能希望將其更改爲:

java.util.List<java.io.File> files = new ArrayList<>(); 

,並添加文件如:

files.add(file); 

這樣你就不必知道的數提前入場。

+0

以秒爲單位打敗我。 +1 –

+1

這將是問題...我沒有給它一個大小,因爲我設法填充allDocs沒有提供使用allDocs.listFiles()的大小,但我想這將是因爲listFiles()返回一個數組,而我試圖將單個對象包含到數組中。謝謝。 – Lighthat