-2

Hello people of the internet,

I am writing a program which checks files to see if the contents of them have been changed(doing this by looking at the bytes of said file). I am getting an error which i am unsure on how to fix.

My program is not compiling as its saying the method is undefined when i have defined it just above the class. I am unsure why this is happening please help.

The method in question is readFileContent and i am trying to call it on the FileReader class.

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);

    }


}

}
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. As in: please provide the exact error message, and mark the line it is pointing to. Then: please take the time to properly format/indent all of your code. Alone the fact that your inner classes aren't properly indented makes it much harder to read your code. Harder for you, but especially harder for the people you want to help you. – GhostCat Feb 19 '19 at 18:24
  • 1
    Your `readFileContent` isn't in `FileReader`, it's in `HashPanel`. (And the method wouldn't use a `FileReader` anyway. Basically you don't need a `FileReader` at all if you're going to call `Files.readAllBytes`.) – Jon Skeet Feb 19 '19 at 18:25
  • @JonSkeet i see your point. I think i am trying to over complicate things – traineeCode47 Feb 19 '19 at 18:43

1 Answers1

0

Your readFileContent() method appears to be in the HashPanel class, and not in the FileReader class. Also, readFileContent() is defined to take in a String as a parameter, but when you call it, you're passing in a File object, and not a String.

Jordan
  • 2,273
  • 9
  • 16
  • FileReader is a class built into java is it not? How would i call the method on the FileReader to read all bytes – traineeCode47 Feb 19 '19 at 18:42
  • 1
    Yes, FileReader is a class built into Java. But it doesn't have a `readFileContent()` method in it. You can see a few examples of how to read a file into a byte[] [here](https://stackoverflow.com/questions/858980/file-to-byte-in-java). – Jordan Feb 19 '19 at 18:55