In this article we will show you the solution of file.delete not working in java, there are four different possible exceptions that can be thrown by File.delete(); three of those exceptions apply here.
The NoSuchFileException (worth checking for specifically), the IOException, and the SecurityException need to be specifically checked for.
IOExceptions are derived from NoSuchFileException and would be caught anyway.
However, casting it to an IOException will remove relevant information, so you might want to catch it as well.
When a web-based program gets blocked by a security manager, this is generally known as a SecurityException. We will now discuss the idea of how to define file delete not working in java with an example.
Step By Step Guide On File.Delete Not Working In Java :-
ActionListener ban = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int fila = table.getSelectedRow(); String number = (String) modelo.getValueAt(fila, 0); modelo.removeRow(fila); try { removeUser(number); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }; btnBanear.addActionListener(ban); public void removeUser(String number) throws IOException { String lee = null; String usuario = ""; CharSequence aux = number; try { register = new File("Register.txt"); tempFile = new File("Register1.txt"); lector = new BufferedReader(new FileReader(register)); fw = new FileWriter(tempFile); writer = new BufferedWriter(fw); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex.getMessage()); } catch (NullPointerException e) { System.out.println(e.getMessage()); } while ((lee = lector.readLine()) != null) { System.out.println(aux); if (lee.contains(number)) { continue; } else { writer.write(lee); } } lector.close(); fw.close(); writer.close(); registro.delete(); }
- A button called btnBanear is created as the first block of code, which sets up an ActionListener. In the event that this button is clicked, the code contained in the actionPerformed method will be executed.
- A value is retrieved from the first column of the selected row of a table by the code inside the actionPerformed method. There is a variable named number that contains the value of this variable.
- Using the removeRow method from the table's model, the next line removes the selected row from the table.
- A call to the removeUser method is completed with the number variable passed as a parameter.
- Number is the parameter that is passed to the removeUser method. There are also some variables that are setup to read and write files, including a register object for Files and a writer object for BufferedWriters.
- With the help of the BufferedReader object, the code reads each line in the file inside the while loop. The code skips the line containing the number passed as an argument if the number is contained in the line. In any other case, it writes the line to a temporary file by using a buffered writer.
- Once the while loop is completed, the original file will be destroyed by calling the delete method of the File class. Once the temporary file has been renamed, it will be renamed to the original filename using the renameTo method under the File class. A final step in the process is to close the file reader and writer objects.
- As a result of running this code, a selected row is removed from a table as well as the corresponding data from a text file.
Conclusion :-
As a result, we have successfully learned how to define file delete not working in java with an example.
Java provides two methods for deleting a file: File.delete() and Files.deleteIfExists(path) in the java.io package and java.nio.file package, respectively.
The delete() method does not throw a SecurityException. It returns a boolean value. If the file is deleted successfully, it returns true; else, it returns false.
I hope this article on file.delete not working in java helps you and the steps and method mentioned above are easy to follow and implement.