1
I’m trying to delete a file using File
, but I have the following mistake:
The file is already being used by another process.
I also have this error when trying to rename using Files
.
How can I "close" a Java file and this error does not occur?
Reading Code
public static String lerPasta() throws FileNotFoundException{
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.getName().endsWith(".xml");
}
};
File dir = new File(diretorioIn);
String texto = null;
File[] files = dir.listFiles(filter);
for(int i = 0; i < files.length ; i++){
texto = new Scanner(new File(files[i].toString()), "UTF-8").useDelimiter("\\A").next();
verificaTipoXML(files[i].toString());
System.out.println("lerPasta():" + files[i].toString());
System.out.println("verificaTipoXML():" + files[i].toString().substring(diretorioIn.length()+1));
deleta = files[i].toString();
deleta2 = files[i].toString().substring(diretorioIn.length()+1);
System.out.println(deleta2);
nomegerado = files[i].toString().substring(diretorioIn.length()+4);
System.out.println("GLOBAL:nomegerado:" + nomegerado);
}
return texto;
}
Return and Delete Code
public static String gerarArquivoRetorno(String string){
File arquivo = new File(diretorioOut+nomegerado);
try( FileWriter fw = new FileWriter(arquivo) ){
fw.write(string);
fw.flush();
System.out.println("gerarArquivoRetorno():" + diretorioOut+nomegerado);
System.out.println("GLOBAL:deleta:" + deleta2);
/*
boolean file;
file = new File(deleta).delete();
System.out.println("gerarArquivoRetorno:file:" + file);
*/
new File(deleta).renameTo(new File(deleta+".BAD"));
//new File(deleta).delete();
Path source = FileSystems.getDefault().getPath(diretorioIn, deleta2);
try {
Files.move(source, source.resolveSibling(deleta+".BAD"));
} catch (IOException e) {
e.printStackTrace();
}
/* Path path = FileSystems.getDefault().getPath(diretorioIn, deleta2);
path = FileSystems.getDefault().getPath(diretorioIn, deleta2);
try {
Files.delete(path);
} catch (IOException | SecurityException e) {
System.err.println(e);
}
*/
}catch(IOException ex){
ex.printStackTrace();
}
return null;
}
A question, you have the file opened in the taskbar?
– DiegoAugusto
Please enter your code!
– Christian Felipe
This looks like an operating system lock, and this even java is not able to undo. You should find some program that might be running in the background with the open file, but I think it would escape some of the scope of the site.
– user28595
@Diegoaugusto no, I opened the file with Java, viewed the info, and now need to delete it
– Lucas Torres
@diegofm no, I opened the file with Java, viewed the info, and now I need to delete it. So I think it’s java
– Lucas Torres
@Christianfelipe ok
– Lucas Torres
See in the task manager if there are no "java.exe" processes running. It may be that you ran and it was not finished properly.
– user28595
@diegofm do not exist. I even restarted the machine
– Lucas Torres
Related question: http://answall.com/q/173484/132
– Victor Stafusa