[英]method is defined and not being recognized by the class
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.有问题的方法是 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);
}
}
}
Your readFileContent()
method appears to be in the HashPanel
class, and not in the FileReader
class.您的
readFileContent()
方法似乎在HashPanel
类中,而不是在FileReader
类中。 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
.此外,
readFileContent()
被定义为接受一个String
作为参数,但是当你调用它时,你传入的是一个File
对象,而不是一个String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.