Error renaming txt file in JAVA

Asked

Viewed 80 times

-1

Hello! I need to rename a file txt, that txt is scheme for system log. Each day has its own log.txt. I am using the following parameters to validate the file and rename, if the file exists and if the first line of the file is the current date, if not, the file should be renamed (It was the only way I found, also accepted suggestions to do better).

Code

   //connTXT.getPath() = "C:\\Users\\" + nomePc + "\\Documents\\Logs\\log.txt";
   File file = new File(connTXT.getPath());


   if(file.exists() && !linha.equals(dataAtual)){

   //connTXT.getDiretorioPastaLog() = "C:\\Users\\" + nomePc + "\\Documents\\Logs";

    // Renomear o arquivo
   file.renameTo(new File(connTXT.getDiretorioPastaLog(),"Teste.txt")); // <== Dúvidas aqui

    }

No visible errors, just do not rename the file.

1 answer

0

You can create a temporary folder 'tmpLog' and put the log.txt on it temporarily and after checking the date and the first line, you can use the File.move(); to a permanent folder.

  • I’ll check, although it’s been busy here these days and I haven’t touched the project again. As soon as I get this weekend I’ll share it with you. I will try the technique of creating a temporary folder or even using the folder temp windows. I will also debug to check if the file is open at the time of renaming. Thanks to all.

Browser other questions tagged

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