My IDE is showing "undeclared FileNotFoundException must be caught or thrown"
- by Dan Czarnecki
I am having the following issue above.
I have tried actually putting a try-catch statement into the code as you will see below, but I can't get the compiler to get past that.
import java.io.*;
public class DirectoryStatistics extends DirectorySize
{
/*
Dan Czarnecki
October 24, 2013
Class variables:
private File directory
A File object that holds the pathname of the directory to look in
private long sizeInBytes
A variable of type long that holds the size of a file/directory (in bytes)
private long fileCount
A variable of type long that holds the number of files in a directory
Constructors:
public DirectoryStatistics(File startingDirectory) throws FileNotFoundException
Creates a DirectoryStatistics object, given a pathname (inherited from DirectorySize class),
and has 3 instance variables that hold the directory to search in, the size of each file (in bytes),
and the number of files within the directory
Modification history:
October 24, 2013
Original version of class
*/
private File directory;
private long sizeInBytes;
private long fileCount;
public DirectoryStatistics(File startingDirectory) throws FileNotFoundException
{
super(startingDirectory);
try
{
if(directory == null)
{
throw new IllegalArgumentException("null input");
}
if(directory.isDirectory() == false)
{
throw new FileNotFoundException("the following input is not a directory!");
}
}
catch(IOException ioe)
{
System.out.println("You have not entered a directory. Please try again.");
}
}
public File getDirectory()
{
return this.directory;
}
public long getSizeInBytes()
{
return this.sizeInBytes;
}
public long getFileCount()
{
return this.fileCount;
}
public long setFileCount(long size)
{
fileCount = size;
return size;
}
public long setSizeInBytes(long size)
{
sizeInBytes = size;
return size;
}
public void incrementFileCount()
{
fileCount = fileCount + 1;
}
public void addToSizeInBytes(long addend)
{
sizeInBytes = sizeInBytes + addend;
}
public String toString()
{
return "Directory" + this.directory + "Size (in bytes) " + this.sizeInBytes + "Number of files: " + this.fileCount;
}
public int hashCode()
{
return this.directory.hashCode();
}
public boolean equals(DirectoryStatistics other)
{
return this.equals(other);
}
}
import java.io.*;
import java.util.*;
public class DirectorySize 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 holds the parent directory
Constructors:
public DirectorySize(File startingDirectory) throws FileNotFoundException
Creates a DirectorySize object, takes in a pathname (inherited from DirectoryProcessor class,
and has a single vector of a DirectoryStatistics object to hold the files and folders
within a directory
Modification History
October 17, 2013
Original version of class
Implemented run() and processFile() methods
*/
private Vector<DirectoryStatistics> directory;
/*
private Vector<Long> directorySizeList;
private Vector<File> currentFile;
*/
public DirectorySize(File startingDirectory) throws FileNotFoundException
{
super(startingDirectory);
directory = new Vector<DirectoryStatistics>();
}
public void processFile(File file)
{
DirectoryStatistics parent;
int index;
File parentFile;
System.out.println(file.getName());
System.out.println(file.getParent());
parentFile = file.getParentFile();
parent = new DirectoryStatistics(parentFile);
System.out.println(parent);
parent.equals(parent);
index = directory.indexOf(parent);
if(index == 0)
{
directory.elementAt(index).addToSizeInBytes(file.length());
directory.elementAt(index).incrementFileCount();
}
if(index < 0)
{
directory.addElement(parent);
directory.lastElement().setSizeInBytes(file.length());
directory.lastElement().incrementFileCount();
}
Could someone tell me why I'm getting this issue?