Java 方法已定义,但该类无法识别该方法
各位网友好, 我正在写一个程序,通过查看文件的字节来检查文件的内容是否发生了变化。我得到一个错误,我不知道如何修复 我的程序没有编译,因为当我在类上面定义方法时,它说方法是未定义的。我不确定为什么会发生这种情况,请帮忙 所讨论的方法是readFileContent,我试图在FileReader类上调用它Java 方法已定义,但该类无法识别该方法,java,class,methods,undefined,Java,Class,Methods,Undefined,各位网友好, 我正在写一个程序,通过查看文件的字节来检查文件的内容是否发生了变化。我得到一个错误,我不知道如何修复 我的程序没有编译,因为当我在类上面定义方法时,它说方法是未定义的。我不确定为什么会发生这种情况,请帮忙 所讨论的方法是readFileContent,我试图在FileReader类上调用它 public class HashPanel extends JPanel { HashPanel() { JButton openFileButton = new JButton(
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);
}
}
}
readFileContent方法似乎位于HashPanel类中,而不是FileReader类中。此外,readFileContent被定义为接受字符串作为参数,但当您调用它时,您传递的是文件对象,而不是字符串。请阅读如何创建字符串。然后使用链接改进您的问题,不要通过评论添加更多信息。否则,我们无法回答您的问题并帮助您。如:请提供准确的错误消息,并标记它所指向的行。然后:请花时间正确格式化/缩进所有代码。仅仅是内部类没有正确缩进这一事实就使得读取代码变得更加困难。对你来说更难,但对你想帮助你的人来说更难。你的readFileContent不在FileReader中,而是在HashPanel中。而且该方法无论如何都不会使用文件读取器。如果要调用Files.readAllBytes,基本上不需要文件阅读器。@JonSkeet我明白你的意思。我想我是在试图把事情复杂化FileReader是java内置的一个类,不是吗?我如何调用FileReader上的方法来读取所有字节是的,FileReader是Java中内置的一个类。但是它没有readFileContent方法。您可以看到一些如何将文件读入字节[]的示例。