How to close a file in Java?

Asked

Viewed 1,181 times

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?

  • Please enter your code!

  • 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.

  • @Diegoaugusto no, I opened the file with Java, viewed the info, and now need to delete it

  • @diegofm no, I opened the file with Java, viewed the info, and now I need to delete it. So I think it’s java

  • @Christianfelipe ok

  • 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.

  • @diegofm do not exist. I even restarted the machine

  • 1

    Related question: http://answall.com/q/173484/132

Show 4 more comments

1 answer

2


Then you have to close the Scanner for the "File" to be released, so it is likely that you will be able to delete your file.

        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++){

            Scanner s = new Scanner(new File(files[i].toString()), "UTF-8");
            texto = s.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);
            s.close();
         }
        return texto;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.