方法已定义但未被类识别

时间:2019-02-19 18:22:23

标签: java class methods undefined

互联网的大家好,

我正在编写一个程序来检查文件,以查看文件的内容是否已更改(通过查看所述文件的字节来执行此操作)。我收到错误消息,不确定如何解决。

我的程序没有编译,因为当我在类上方定义该方法时,该方法未定义。我不确定为什么会发生这种情况,请帮忙。

有问题的方法是readFileContent,我正在尝试在FileReader类上调用它。

public class HashPanel extends JPanel {


HashPanel() {
    JButton openFileButton = new JButton("Open a File");
    openFileButton.setToolTipText("Open a File");
    openFileButton.addActionListener(new OpenFileListner());

    JButton openDirButton = new JButton("Open a Directory");
    openDirButton.setToolTipText("Open a Directory");
    openDirButton.addActionListener(new OpenDirListner());

    add(openFileButton);
    add(openDirButton);


    //textArea.setEditable(false);
}

public byte[] readFileContent(String pathName) throws IOException {

    return Files.readAllBytes(Paths.get(pathName));
}


class OpenFileListner implements ActionListener{
public void actionPerformed(ActionEvent e){
    try {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(chooser);
        File file = chooser.getSelectedFile();

        String fileName = file.getName();

        FileReader reader = new FileReader(file);

        byte[] bytes = reader.readFileContent(file);

        int length = bytes.length;

        System.out.println("File length is " + length);

        long total = 0;

        // Dump byte array contents and total the values.
        for (byte b : bytes) {

            System.out.println(String.format("%X", b));

            total += b * 13;
        }

        // create a very simple hash (total of byte values, each multiplied by a prime number, all of which is multiplied by file size)
        total *= length * 997;


        JOptionPane.showMessageDialog(null, "File Name:  " + fileName + "\nFile Length: "+ length+"\nHash:  "+total);
    } catch (Exception a) {
        JOptionPane.showMessageDialog(null, "Select a file");
    }

}
}



class OpenDirListner implements ActionListener{
public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.showOpenDialog(chooser);

    String dirName = chooser.getSelectedFile().getAbsolutePath();

    File files = new File(dirName);

    File[] fileName = files.listFiles();


    String names = "";

    if (files != null) {

        // iterate over files and directories
        for (File next : fileName) {

            // Test if file (or directory)
            boolean isFile = next.isFile();

            // Get name and last modified information.
            String name = next.getName();


            names = names + " \n" + name;

        }


        JOptionPane.showMessageDialog(null, "Directory Name:  " + dirName + "\n Filenames:  "+ names);

    }


}

}

1 个答案:

答案 0 :(得分:0)

您的readFileContent()方法似乎在HashPanel类中,而不在FileReader类中。另外,readFileContent()被定义为接受String作为参数,但是当您调用它时,您传入的是File对象,而不是String