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