为什么使用另一个按钮后文件没有被删除

Why file doesn't get deleted after using another button

我editing.I也发现使用其他按钮后无法删除,因为它抛出java.nio.file.FileSystemException: 2018 June Saturday 30 09hr19min37sec.txt: 该进程无权访问文件,因为它正被另一个 process.Which 第二个按钮的代码部分使用,导致系统继续读取或访问文件,因此第一个删除按钮无法删除文件? 我有一个从组合框中删除选定文件的按钮和另一个显示月利润的按钮。问题是,当我点击 borrar(delete) 按钮时,它会正确删除选定的文件,但是当我点击 Ventas del mes(Month profit) 按钮然后我想再次点击 borrar(delete) 按钮时,它不会从组合框中删除所选文件。每次单击月利润后单击删除按钮都会出现这种情况 button.How 我可以解决这个问题吗?

Set<String> results = new HashSet<String>();
Set<String> Año = new HashSet<String>();
Set<String> Mes = new HashSet<String>();
Set<String> Dia = new HashSet<String>();
Set<String> textos = new HashSet<String>();
String[] meses = {"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre",};
Set<String> meses1 = new HashSet<String>();
File[] files = new File("C:\Users\SATELITE\Documents\NetBeansProjects\Restaurante").listFiles();
   public Recibos() {
    initComponents();

    for (File file : files) {
        for (int i = 0; i < meses.length; i++) {
            if (file.getName().contains(meses[i])) {
                meses1.add(meses[i]);
            }
        }
        if (file.isFile()) {
            if (file.getName().contains(".txt")) {
                results.add(file.getName());
            }
        }
    }
    DefaultComboBoxModel DefaultComboBoxModel2 = new DefaultComboBoxModel(meses1.toArray());
    cbMes.setModel(DefaultComboBoxModel2);
}

然后我有第一个按钮

private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    txtMostrar.setText("");
    results.clear();
    System.out.println(results);
    String text = cbRecibo.getSelectedItem().toString();
    File[] files = new File("C:\Users\SATELITE\Documents\NetBeansProjects\Restaurante").listFiles();
    for (File file : files) {
        if (file.isFile()) {
            if (file.getName().equals(text)) {
                Path p1 = Paths.get(text);
                try {
                    java.nio.file.Files.deleteIfExists(p1);
                } catch (IOException ex) {
                    Logger.getLogger(Recibos.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println(file + "xd1");
            }
        }
    }
    System.out.println(results+"results");
    File[] files2 = new File("C:\Users\SATELITE\Documents\NetBeansProjects\Restaurante").listFiles();
    for (File file : files2) {
        if (file.isFile()) {
            if (file.getName().contains(".txt")) {
                System.out.println(file + "xd2");
                results.add(file.getName());
            }
        }
    }
    System.out.println(results);
    cbRecibo.removeAll();
    DefaultComboBoxModel DefaultComboBoxModel1 = new DefaultComboBoxModel(results.toArray());
    cbRecibo.setModel(DefaultComboBoxModel1);
}

第二个按钮在点击后导致第一个按钮没有按应有的方式删除。

private void btnVentasMesActionPerformed(java.awt.event.ActionEvent evt) {                                             
    suma = 0;
    List<String> list = new ArrayList<String>();
    Iterator<String> iterator = results.iterator();

    while (iterator.hasNext()) {
        String setElement = iterator.next();

        File file = new File(setElement);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                if (text.contains("Total")) {
                    list.add(text);
                }
            }
        } catch (Exception e) {
        }

    }


    //good way:
    Iterator<String> iterator2 = list.iterator();
    while (iterator2.hasNext()) {
        String setElement = iterator2.next();
        String numberOnly = setElement.replaceAll("[A-Z,a-z,:]", "");
        suma = suma + Double.parseDouble(numberOnly);


    }

    String totalDeVentas = "La venta total del mes de " + cbMes.getSelectedItem().toString() + "\n fue de :" + suma;
    txtMostrar.setText(totalDeVentas);

}  

改为

if (file.getName().equals(text)) {
     file.delete();
}

但看起来你也在吞下任何错误

 } catch (Exception e) {
 }

改为

 } catch (Exception e) {
      e.printStackTrace();
 }

我已经解决了 it.I 没有关闭这个 reader

reader = new BufferedReader(new FileReader(file));
reader.close();

感谢您的帮助。