I am trying to move a file that is inside an Array using renameTo

Asked

Viewed 30 times

1

Hi, I’m new to java and I’m having a hard time using "renameTo". and in this activity I have to move files do not meet the following requirements

  1. Is not empty;
  2. Each row of the file has the exact amount of expected columns to be imported

So far so good! My difficulty is in moving to the folders! Follows the code

I’m picking up files . csv here!

try {
         File arquivos[];
            File diretorio = new File("C:\\Users\\akyri\\Documents\\NetBeansProjects\\LeitoDeCsv\\PENDENTES");
            arquivos = diretorio.listFiles();
         
            for(int i = 0; i < arquivos.length; i++){
                   //leia arquivos[i];
                
                //Pega o arquivo e manda ler
              ConteudoCSV = new BufferedReader(new FileReader(arquivos[i]));

And here I am checking and displaying the code

 while((linha = ConteudoCSV.readLine()) != null){
                  //Trata a linha vazia que está no CSV!
                  if( linha.trim().isEmpty() ) {
                      continue;
                  }
                    
                  
                  //Vai "contar" a linha a cada ";"
              String[] venda = linha.split(csvSeparadorCompo);
              
              var quant = venda.length;
              if(quant != 4) {
                  
                
                  
                   boolean ok = arquivos[i].renameTo(new File(DiretorioInvalidos,arquivos[i].getName()));
                   if(ok){
                        System.out.println("Arquivo foi movido com sucesso");
                    }
                   else{
                        System.out.println("Nao foi possivel mover o arquivo");
                    }
              
              }
            
              System.out.println("[venda = " + venda[0]
                                + ", Nome = " + venda[1]
                                + ", Data = " + venda[2]
                                + ", Preço = " + venda[3]);
              
              
                  
          
            }    
            }

The problem is that Boolean is always returning false! Someone tells me what I’m doing wrong. Grateful!

1 answer

0

There is a good chance that there is a file conflict with the same name in the destination folder, so the method returns false.

Browser other questions tagged

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