java - 文件名未从不同类添加到 ArrayList

标签 java list class arraylist filenames

我在 checksumFinder 类中有一个方法 hexFinder。在 hexFinder 中,每个文件都会被读取并命名为文件或文件。然后将其放入两个 ArrayList 之一:listGoodFileslistBadfiles

这两个 ArrayList 都属于 multiFile 方法中的 checksumGUI 类。此方法为所选文件夹内的每个文件调用hexFinder。它还输出一条消息,显示文件夹中有多少文件。

当我将文件名添加到其中一个 ArrayList 时,没有添加任何内容。

以下是 checksumFinder 类中可能导致问题的一些代码

class checksumFinder {

checksumGUI cg = new checksumGUI();

String hexFinder(File currentFile,...){

.... // determine file is good/bad 

if (l1 == l2) {
    cg.listGoodFiles.add(currentFile.getName());
    } else{
      cg.listBadFiles.add(currentFile.getName());
    }

这是 checksumGUI 类中的 multiFile 方法

public void multiFile(JFileChooser inFileName) throws FileNotFoundException, IOException {

    checksumFinder cf = new checksumFinder();
    ArrayList<String> listTypeErrFiles = new ArrayList<String>();
    File folderFile = inFileName.getSelectedFile();
    File[] listAllFiles = folderFile.listFiles();

    for (int i = 0; i < listAllFiles.length; i++) {
        File currentFile = listAllFiles[i];
        if (currentFile.isFile() && currentFile.getName().endsWith(".pcf")) {

            cf.hexFinder(currentFile, null, null, null);
        } else {
            listTypeErrFiles.add(currentFile.getName());
            System.out.println("-----------------------------");
            System.out.println("Incorrect filetype:\n" + currentFile.getName());
            System.out.println("-----------------------------\n");
        }
    }

    JTextArea ta = new JTextArea(25, 25);
    ta.setText("Folder contains " + listAllFiles.length + " files\n\n"+ "Folder has " + 
            listGoodFiles.size() + " good files;\n" + listGoodFiles + "\nFolder has " +
            listGoodFiles.size() + " bad files;\n" + listBadFiles +"\nFolder has " + 
            listTypeErrFiles.size() + " file of wrong type;\n" + listTypeErrFiles);

            ta.setWrapStyleWord(true);
            ta.setLineWrap(true);
            ta.setCaretPosition(0);
            ta.setEditable(false);

            JOptionPane.showMessageDialog(null,
            new JScrollPane(ta),"Folder Contents", JOptionPane.INFORMATION_MESSAGE);
}

从我制作这个程序的方式来看,两个类之间有很多变化。 (我确信它可以做得更好,但这是我知道的唯一方法)

编辑:

我已经浏览过类似的问题,例如 this但我发现没有任何有用的东西

编辑:

checksumFinder 类的(最新)代码可以在 HERE 中找到。 .

非常感谢任何帮助,谢谢。

最佳答案

您正在创建一个新的 checksumGUI checksumFinder 中的实例而不是访问现有实例:

cg = new checksumGUI();

您应该将现有实例作为构造函数的参数:

class checksumFinder {
    private final checksumGUI cg;

    checksumFinder(checksumGUI cg) {
        this.cg = cg;
    }

    ...
}

而不是 checksumFinder cf = new checksumFinder();multiFile() ,将当前实例传递给 checksumFinder构造函数:

checksumFinder cf = new checksumFinder(this);

(此外,使用 java 编码约定,例如大写的类名,将使代码更易于阅读)。

关于java - 文件名未从不同类添加到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721849/

相关文章:

java - 如何解决无法在 Maven 项目中内省(introspection)测试类的警告?

JAVA - AES key 长度不匹配

java - 在Java中开发泛型修饰符的正确方法

java - Maven pom 文件继承范围

使用 arquillian+omnifaces 运行测试时出现 java.util.ServiceConfigurationError

c - 带 c 的双链表

python - 无法使用基类创建其子类的实例

python - 在 python 列表中查找元组中的唯一元素

java - 如何在java列表中使用addall

css - Blueprint 的最后一个类是否存在已知问题?