How to close a file in Java?

1

I'm trying to delete a file using File , but I get the following error:

  

The file is already being used by another process.

I also get this error when trying to rename using Files .

How can I "close" a file in Java 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 Exclude 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;    
    }
    
asked by anonymous 26.12.2016 / 14:56

1 answer

2

Then you have to close the Scanner so that the "File" is released, this way you are likely to 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;
    
26.12.2016 / 17:14