2013-10-24 49 views
0

我遇到了上面提到的問題。無法使用我的文件程序計算目錄的總大小

供您參考,這是我這個程序代碼:

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

public class DirectorySizeProcessor extends DirectoryProcessor 
{ 
    /* 
    Dan Czarnecki 
    October 17, 2013 

    Class variables: 
     private Vector<Long> directorySizeList 
      Variable of type Vector<Long> that holds the total file size of files in that directory 
      as well as files within folders of that directory 

     private Vector<File> currentFile 
      Variable of type Vector<File> that 

    Modification History 
     October 17, 2013 
      Original version of class 
      Implemented run() and processFile() methods 
    */ 

    private Vector<Long> directorySizeList; 
    private Vector<File> currentFile; 

    public DirectorySizeProcessor(File startingDirectory) throws FileNotFoundException 
    { 
     super(startingDirectory); 
     directorySizeList = new Vector<Long>(); 
     currentFile = new Vector<File>(); 
    } 

/* 
    public void run() 
    { 
     File file; 
     file = directoryLister.next(); 
     while(file != null) //the following will be called when the File object is not null 
     { 
      processFile(file); 
      file = directoryLister.next(); 
     } 
    } 
*/ 


    public void processFile(File file) 
    { 
     Long hold; 
     Long currentSize; 
     Long finalTotal; 
     int index; 
     File parentFile; 

     parentFile = file.getParentFile(); 
     index = this.currentFile.indexOf(parentFile); 

     if(index < 0) 
     { 
      this.directorySizeList.addElement((long)0); 
      this.currentFile.addElement(file.getParentFile()); 
     } 
     while(index > 0) 
     { 
      currentSize = this.directorySizeList.get(index); 
      hold = file.length(); 
      finalTotal = hold + currentSize; 
      //System.out.println("current size: " + parentFile); 
      //System.out.println("current size: " + currentSize); 
      System.out.println("file: " + file); 
      System.out.println("current size: " + file.length()); 
      parentFile = parentFile.getParentFile(); 
      index = this.getParentDirectory().indexOf(parentFile); 
      finalTotal = file.length() + finalTotal; 
      System.out.println("final total: " + finalTotal); 
     } 

    } 

    public void run() 
    { 
     File file; 
     file = directoryLister.next(); 


     while(file != null) //the following will be called when the File object is not null 
     { 
      processFile(file); 
      file = directoryLister.next(); 
     } 

    } 

    public Vector getParentDirectory() 
    { 
     return this.currentFile; 
    } 

    public Vector getDirectorySizeList() 
    { 
     return this.directorySizeList; 
    } 


} 

我知道,問題主要在於我processFile()方法中。當我從我的Driver類(通過run()方法,它調用processFile()方法)中運行它時,它會正確顯示每個單獨文件的大小(以字節爲單位),但我認爲它不計算整個目錄的大小,因爲它與在屬性窗口中顯示的大小不匹配。有人可以幫我在這裏嗎?

回答

0

我想你忘了在向矢量添加一個新的目錄/大小後設置索引,這可能會導致它總是跳過任何目錄的第一個文件。

... 
    index = this.currentFile.indexOf(parentFile); 

    if(index < 0) 
    { 
     this.directorySizeList.addElement((long)0); 
     this.currentFile.addElement(file.getParentFile()); 
     // set the index again to make it enter the while loop 
     index = this.currentFile.indexOf(parentFile); 
    } 
    while(index > 0) 
    { 
    ...