0

我在上面遇到以下问题。

正如您将在下面看到的那样,我实际上已经尝试将 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();
        }

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

4

2 回答 2

0

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

于 2013-10-28T21:57:11.783 回答
0

我认为编译器抱怨您正在实例化DirectoryStatisticsor的语句DirectorySize

问题是这样的。您已声明DirectoryStatisticsandDirectorySize 可以抛出FileNotFoundException; 例如

 public DirectoryStatistics(File startingDirectory) 
     throws FileNotFoundException

 public DirectorySize(File startingDirectory) 
     throws FileNotFoundException

由于您已经声明了这一点,并且由于它是一个检查异常,因此您需要在构造函数中“处理”该异常。

在您的DirectoryStatistics构造函数中,您尝试处理异常。然而,这还不够。

  • 构造函数中的super调用DirectoryStatistics正在调用DirectorySize构造函数。

  • 超级构造函数抛出该声明。

  • 超级调用不在.try / catch

  • 不能把它放在那里,因为 Java 语法规则不允许它。显式(或隐式)超级调用必须是构造函数的第一条语句。

  • 即使你这样做了,你已经将构造函数声明DirectoryStatistics抛出异常的事实意味着该构造函数的调用者必须处理它。(此版本的)构造函数可能不允许传播异常是无关紧要的。


在此特定示例中,您可以通过删除两个构造函数throws上的 来“修复”此问题。但这假设构造函数也不会抛出异常。DirectoryProcessor

通常,如果超类构造函数throws检查了异常,那么链接该构造函数的任何子类构造函数都别无选择,只能选择throws相同的异常……或异常的超类。(如果您从对象封装的角度考虑这一点,Java 以这种方式工作是一件好事。)

于 2013-10-28T22:39:25.507 回答