gpt4 book ai didi

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

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

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

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();
}

关于java - 为什么使用另一个按钮后文件没有被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51093136/

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