Skip file when entering some Exception

1

Good afternoon

Below is a method that I use to read certain files in a folder. The problem is this. If the folder has 50 files (or 60, or 70, or 80 ...) and the method is reading a "defective" file (one of the files that fall in Exeception ), the method locks on that file and does not consume the files. others. I would like that when entering a exception , the stream would continue, and then return to that file. If it is OK, it consumes, if not, jump back to the others.

public static String lerPasta() throws FileNotFoundException{
      String texto = null;  
        try{    
            FileFilter filter = new FileFilter() {
            public boolean accept(File file) {
                return file.getName().endsWith(".XML");
                }
            };
            File dir = new File(diretorioIn);
               File[] files = dir.listFiles(filter);

            for(i = 0; i < files.length ; i++){
                Scanner s = new Scanner(new File(files[i].toString()), "UTF-8");
                texto = s.useDelimiter("\A").next();     
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"VALOR DE I" +i);
                verificaTipoXML(files[i].toString().substring(diretorioIn.length()));
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML:files[i].toString(): "+files[i].toString());
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" lerPasta():" + files[i].toString());
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML():" + files[i].toString().substring(diretorioIn.length()+0));
                deleta = files[i].toString();
                //+1
                deleta2 = files[i].toString().substring(diretorioIn.length());
                deletar = files[i];
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" deleta:"+deleta2);
                nomegerado = files[i].toString().substring(diretorioIn.length()+3);
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" GLOBAL:nomegerado:" + nomegerado);
                s.close();
             }

        }catch(FileNotFoundException e){
                 System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: FileNotFoundException : Tentando novamente ...");
        }    
        catch(NoSuchElementException e){
             System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: NoSuchElementException : Tentando novamente ...");

        }
        return texto;
    }
    
asked by anonymous 16.01.2017 / 19:56

2 answers

1

Your method is crashing because your try / catch is out of order and when an exception is thrown, the loop is broken. You simply move the try / catch into the for and it will try to read all the files.

In order for it to try to read the files that fail again, you can store them in a list and then try to read them again. It would look something like this:

public static String lerPasta() {

    FileFilter filter = new FileFilter() {
        public boolean accept(File file) {
            return file.getName().endsWith(".pdf");
        }
    };

    File dir = new File(FILE_PATH);
    Iterator<File> files = Arrays.asList(dir.listFiles(filter)).iterator(); 
    List<File> failedFiles = new ArrayList<>();

    while(files.hasNext()) {
        File file = files.next();
        try(Scanner s = new Scanner(new File(file.toString()), "UTF-8")) {
            //Faça o que quiser aqui
            //...
        } catch (FileNotFoundException | NoSuchElementException e) {
            if(failedFiles != null) {
                failedFiles.add(file);
            }
        } finally {
            if(!files.hasNext() && failedFiles != null && !failedFiles.isEmpty()) {
                files = failedFiles.iterator();
                failedFiles = null;
            }
        }
    }

}
    
16.01.2017 / 23:45
0

It will crash because the for is inside the try-catch, as soon as the exception is thrown ( FileNotFoundException or NoSuchElementException ), it will exit the for and enter the catch.

Try to do that. I took it out from inside the catch, and put another one inside it (from for). This try-catch inside for will ignore the "faulty" files.

Just to remember, it's not cool to be using pure try-catch, that is, hiding exception, the reason has to be very good.

public static String lerPasta() throws FileNotFoundException{
      String texto = null; 
      File[] files = null;    
        try{    
            FileFilter filter = new FileFilter() {
            public boolean accept(File file) {
                return file.getName().endsWith(".XML");
                }
            };
            File dir = new File(diretorioIn);
               File[] files = dir.listFiles(filter);

        }catch(FileNotFoundException e){
                 System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: FileNotFoundException : Tentando novamente ...");
        }    
        catch(NoSuchElementException e){
             System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: NoSuchElementException : Tentando novamente ...");
        }

        if(files == null){
            //Lança algum erro.
        }

        for(i = 0; i < files.length ; i++){
            try{
                Scanner s = new Scanner(new File(files[i].toString()), "UTF-8");
                texto = s.useDelimiter("\A").next();     
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"VALOR DE I" +i);
                verificaTipoXML(files[i].toString().substring(diretorioIn.length()));
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML:files[i].toString(): "+files[i].toString());
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" lerPasta():" + files[i].toString());
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML():" + files[i].toString().substring(diretorioIn.length()+0));
                deleta = files[i].toString();
                //+1
                deleta2 = files[i].toString().substring(diretorioIn.length());
                deletar = files[i];
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" deleta:"+deleta2);
                nomegerado = files[i].toString().substring(diretorioIn.length()+3);
                System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" GLOBAL:nomegerado:" + nomegerado);
            }catch(AlgumaException e){
                //Não faz nada.
            }finally{
                s.close();
            }
        }
    return texto;
}
    
16.01.2017 / 23:02