gpt4 book ai didi

java - java访问文件时出现错误

转载 作者:行者123 更新时间:2023-12-02 05:08:16 26 4
gpt4 key购买 nike

我正在编写一个程序来检查重复文件并将结果显示在表格中。我已经设置了一个表格模型,还设置了一个按钮,该按钮将触发程序检查重复文件并将其显示在表格上。但是我在表模型上显示它们时遇到错误。这是我的代码:

//This is the code that checks for duplicate files and displays it on the table model 
public void findDuplicateFiles(File[] files) throws IOException {

Map<String, List<File>> filesByHash = new HashMap<>();
for (File file : files) {
if (!file.isFile()) {
continue;
}

String hash = MD5.asHex(MD5.getHash(file));

List<File> filesForHash = filesByHash.get(hash);
if (filesForHash == null) {
filesByHash.put(hash, filesForHash = new ArrayList<>());
}

filesForHash.add(file);

}

for (Map.Entry<String, List<File>> entry : filesByHash.entrySet()) {
List<File> filesForHash = entry.getValue();
if (filesForHash.size() > 1) {
String hash = entry.getKey();
System.out.printf("%,d files have hash %s:%n",
filesForHash.size(), hash);
int index = filesForHash.size() - 1;
filesForHash.remove(index);

for (File file : filesForHash) {

System.out.println(" " + file);

fileTableModel.setFiles(file.listFiles());
}

}
//System.out.println(" No Duplicate Found ");

}

}


//This is the code that triggers a button and calls the above method for duplicate

checkDup = new JButton("Find Duplicate");
checkDup.setMnemonic('t');
checkDup.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
findDuplicateFiles(currentFile.listFiles());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
toolBar.add(checkDup);



//Here is the Table Model code.

class FileTableModel extends AbstractTableModel {

public File[] files;
public FileSystemView fileSystemView = FileSystemView.getFileSystemView();
public String[] columns = {
"Icon",
"File",
"Path/name",
"Size",
"Last Modified",

};

FileTableModel() {
this(new File[0]);
}

FileTableModel(File[] files) {
this.files = files;
}

public Object getValueAt(int row, int column) {
File file = files[row];
switch (column) {
case 0:
return fileSystemView.getSystemIcon(file);
case 1:
return fileSystemView.getSystemDisplayName(file);
case 2:
return file.getPath();
case 3:
return file.length();
case 4:
return file.lastModified();
default:
System.err.println("Fatal Error");
}
return "";
}

public int getColumnCount() {
return columns.length;
}

public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return ImageIcon.class;
case 3:
return Long.class;
case 4:
return Date.class;

}
return String.class;
}

public String getColumnName(int column) {
return columns[column];
}

public int getRowCount() {
return files.length;
}

public File getFile(int row) {
return files[row];
}

public void setFiles(File[] file) {
this.files = file;
fireTableDataChanged();
}

}

这是我单击按钮时收到的错误代码:

        2 files have hash Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
da8f60e8474f7c89f368e5d6d379dcdc:
C:\Users\Mbaegbu\Documents\Bandicam\bandicam 2014-07-02 10-55-03-421 - Copy.jpg
at com.twmacinta.util.FileTableModel.getRowCount(DupFileBrowser.java:900)
at javax.swing.JTable$ModelChange.<init>(Unknown Source)
at javax.swing.JTable.sortedTableChanged(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableDataChanged(Unknown Source)
at com.twmacinta.util.FileTableModel.setFiles(DupFileBrowser.java:909)
at com.twmacinta.util.DupFileBrowser.findDuplicateFiles(DupFileBrowser.java:418)
at com.twmacinta.util.DupFileBrowser$10.actionPerformed(DupFileBrowser.java:325)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)

我们将不胜感激您的帮助。

最佳答案

您将调用 File.listFiles() 的结果传递给表模型的 setFiles() 方法。如果调用 listFiles() 的文件不是目录,则 listFiles() 将返回 null。这会导致您遇到空指针异常。

关于java - java访问文件时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27573841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com