Home » Java » java – File.delete and File.renameTo not working after closing file-Exceptionshub

java – File.delete and File.renameTo not working after closing file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to delete an x file and rename a y file with the name of the x file but even when i close all the files it doesn’t delete nor rename the files.

try {
                boolean buffer = false;
                boolean finder = false;
                 PrintWriter output = new PrintWriter(new FileOutputStream(file, true));
                 PrintWriter output2 = new PrintWriter(new FileOutputStream(file4, true));
                 LineNumberReader readers = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                 LineNumberReader readers2 = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

                 while((line = readers2.readLine()) != null)
                 {
                     if(line.contains(CL.getID()))
                     {
                         finder = true;
                         line = "";
                         break;
                     }


                 }

                 if(finder == false)
                 {
                     System.out.println("ID NO ENCONTRADO!!!");
                     output.close();
                     output2.close();
                     readers.close();
                     readers2.close();
                     break;
                 }


                 while((line = readers.readLine()) != null)
                 {
                     if(line.contains(CL.getID()))
                     {
                         buffer = true;
                         output2.println(line);
                         System.out.println("Escriba el nuevo presupuesto ");
                         CL.setPresupuesto(reader.next());
                         output2.println("Presupuesto: " + CL.getPresupuesto() + "$");



                     }
                     else
                     {
                         if(buffer == true)
                         {
                             buffer = false;
                             continue;
                         }
                         output2.println(line);
                     }
                 }

                 System.out.println("Modificacion Terminada!!!!");
                 output.close();
                 output2.close();
                 readers.close();
                 readers2.close();
                 file.delete();
                 file4.renameTo(file);


            }catch(IOException ex)
            {
                System.out.println("ERROR!!!!");
            }

as you see i have closed the files i used but still does not work:

                 output.close();
                 output2.close();
                 readers.close();
                 readers2.close();
                 file.delete();
                 file4.renameTo(file);

Is it something wrong with the logic of the code or if it just something that i’m missing?

How to&Answers: