Java “我的IDE正在显示”;必须捕获或抛出未声明的FileNotFoundException;

Java “我的IDE正在显示”;必须捕获或抛出未声明的FileNotFoundException;,java,filenotfoundexception,Java,Filenotfoundexception,我有以下问题上面 我已经尝试在代码中加入try-catch语句,如下所示,但我无法让编译器通过该语句 import java.io.*; public class DirectoryStatistics extends DirectorySize { /* Dan Czarnecki October 24, 2013 Class variables: private File directory A File object

我有以下问题上面

我已经尝试在代码中加入try-catch语句,如下所示,但我无法让编译器通过该语句

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();
        }
import java.io.*;
公共类DirectoryStatistics扩展了DirectorySize
{
/*
丹扎内基
2013年10月24日
类变量:
专用文件目录
保存要查找的目录路径名的文件对象
专用长尺寸字节
long类型的变量,用于保存文件/目录的大小(以字节为单位)
私有长文件计数
long类型的变量,用于保存目录中的文件数
建造商:
public DirectoryStatistics(文件启动目录)引发FileNotFoundException
在给定路径名(从DirectorySize类继承)的情况下创建DirectoryStatistics对象,
并且有3个实例变量,用于保存要搜索的目录,每个文件的大小(以字节为单位),
以及目录中的文件数
修改历史记录:
2013年10月24日
类的原始版本
*/
私有文件目录;
私有长尺寸字节;
私有长文件计数;
public DirectoryStatistics(文件启动目录)引发FileNotFoundException
{
超级(启动目录);
尝试
{
if(目录==null)
{
抛出新的IllegalArgumentException(“空输入”);
}
if(directory.isDirectory()==false)
{
抛出新的FileNotFoundException(“以下输入不是目录!”);
}
}
捕获(ioe异常ioe)
{
System.out.println(“您尚未输入目录,请重试。”);
}
}
公共文件getDirectory()
{
返回此目录;
}
公共长getSizeInBytes()
{
返回这个.sizeInBytes;
}
公共长getFileCount()
{
返回此.fileCount;
}
公共长setFileCount(长大小)
{
文件计数=大小;
返回大小;
}
公共长setSizeInBytes(长尺寸)
{
sizeInBytes=大小;
返回大小;
}
public void incrementFileCount()
{
fileCount=fileCount+1;
}
public void addToSizeInBytes(长加数)
{
大小字节=大小字节+加数;
}
公共字符串toString()
{
返回“Directory”+this.Directory+“Size(以字节为单位)”+this.sizeInBytes+“文件数:”+this.fileCount;
}
公共int hashCode()
{
返回此.directory.hashCode();
}
公共布尔值等于(目录统计信息其他)
{
返回此。等于(其他);
}
}
导入java.io.*;
导入java.util.*;
公共类DirectorySize扩展了DirectoryProcessor
{
/*
丹扎内基
2013年10月17日
类变量:
私有向量目录SizeList
Vector类型的变量,用于保存该目录中文件的总文件大小
以及该目录文件夹中的文件
专用矢量文件
保存父目录的Vector类型的变量
建造商:
public DirectorySize(File startingDirectory)引发FileNotFoundException
创建DirectorySize对象,接受路径名(从DirectoryProcessor类继承),
并且有一个DirectoryStatistics对象的单个向量来保存文件和文件夹
在目录中
修改历史
2013年10月17日
类的原始版本
实现了run()和processFile()方法
*/
专用向量目录;
/*
私有向量目录;
专用矢量文件;
*/
public DirectorySize(File startingDirectory)引发FileNotFoundException
{
超级(启动目录);
directory=newvector();
}
公共void进程文件(文件文件)
{
目录统计;
整数指数;
文件父文件;
System.out.println(file.getName());
System.out.println(file.getParent());
parentFile=file.getParentFile();
父项=新目录统计信息(父项文件);
System.out.println(父级);
父母。等于(父母);
index=directory.indexOf(父级);
如果(索引==0)
{
directory.elementAt(index.addToSizeInBytes(file.length());
directory.elementAt(index.incrementFileCount();
}
如果(指数<0)
{
addElement目录(父目录);
directory.lastElement().setSizeInBytes(file.length());
directory.lastElement().incrementFileCount();
}

有人能告诉我为什么会出现这个问题吗?

processFile()中
您创建了
DirectoryStatistics
实例。在
DirectoryStatistics
构造函数中,您声明了
FileNotFoundException
。因此,当您尝试istantate
DirectoryStatistics
时,您应该处理此异常,或者在方法签名中声明它。这是检查异常的规则。

请使用编译器在实例化
DirectoryStatistics
DirectorySize
时抱怨的语句

问题是这样的。您已声明
DirectoryStatistics
DirectorySize
可能引发
FileNotFoundException
;例如

 public DirectoryStatistics(File startingDirectory) 
     throws FileNotFoundException

 public DirectorySize(File startingDirectory) 
     throws FileNotFoundException
由于您已经声明了该异常,并且它是一个已检查的异常,因此需要在构造函数中“处理”该异常

在您的
DirectoryStatistics
构造函数中,您试图处理异常。但是,这还不够

  • super
    调用