- #1
peterpiper
- 14
- 0
Homework Statement
You are to write a robust program (i.e., a public static void main()) that accepts a file pathname (e.g. 'c:\foo\bar' or '/home/jblow/' under UNIX) as input and then calculates and prints the size of that filesystem object:
java FileSize c:\foo\bar
123625 bytes
The purpose of the program is to compute the total size in bytes occupied by the given object. For files, this is just the size of the file itself. For directories this will be the recursive sum of the sizes of the objects inside the directory.
Homework Equations
N/A
The Attempt at a Solution
Code:
import java.io.File;
import javax.swing.JFileChooser;
import java.util.Stack;
public class FileSize
{
/**
* FileSize computes the total size of a directory or file.
* @param args
*/
public static void main(String[] args)
{
//Variable Declaration
Stack <File> folderStack = new Stack<File>();
long fileSize = 0;
JFileChooser chooser = new JFileChooser();
//Only allow the user to select a directory for the time being
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = chooser.showOpenDialog(null);
//If a valid file is selected proceed
if (returnValue == JFileChooser.APPROVE_OPTION)
{
//Create a file object from the directory
File selectedFile = chooser.getSelectedFile();
//If the file can be read proceed
if(selectedFile.canRead())
{
if(selectedFile.isDirectory())
{
//If the file object is a directory, push it on a stack to be operated on later
folderStack.push(selectedFile);
//Until the stack is empty, keep calculating directory sizes
while(!folderStack.isEmpty())
{
//List directory contents
File[] listOfFiles = folderStack.pop().listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
//If the item is a file, add to total size
if(listOfFiles[i].isFile())
{
fileSize += listOfFiles[i].length();
System.out.println(fileSize + " bytes.");
}
//if it's a directory, push it onto the stack
else
{
folderStack.push(listOfFiles[i]);
}
}
}
}
//If the selected item is just a file, calculate the size
else
{
fileSize = selectedFile.length();
}
}
else
{
System.out.println("Borked");
}
//Report the size
long fileSizeKB = fileSize / 1024;
System.out.println(fileSize + " bytes.");
}
}
}
Question [\b]
So overall I have the correct functionality(minus the JFileChooser, but that will change), provided there aren't any protected files in the directory, but I keep getting an odd exception thrown after the final reporting of the file size.
Code:
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at sun.java2d.Disposer.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
An even stranger thing is that it only occasionally does it. I don't change code but it only does it may once out of every 6ish runs? By what I gather it's a threading exception but I don't know why I would be popping that here. Any suggestion for how to drop the error?