0

The code basically allows the user to input the name of the file that they would like to delete which is held in the variable 'catName' and then the following code is executed to try and find the path of the file and delete it. However, it doesn't seem to work, as it won't delete the file this way. Is does however delete the file if I input the whole path.

File file = new File(catName + ".txt");
String path = file.getCanonicalPath();
File filePath = new File(path);
filePath.delete(); 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kimberley Lloyd
  • 577
  • 1
  • 5
  • 7

3 Answers3

1

Depending on what file you want to delete, and where it is stored, chances are that you are expecting Java to magically find the file.

String catName = 'test'
File file = new File(catName + '.txt');

If the program is running in say C:\TestProg\, then the File object is pointing to a file in the location C:\TestProg\test.txt. Since the file object is more of just a helper, it has no issues with pointing to a non-existent file (File can be used to create new files).

If you are trying to delete a file that is in a specific location, then you need to prepend the folder name to the file path, either canonically, or relative to the execution location.

String catName = 'test'
File file = new File('myfiles\\'+ catName +'.txt');

Now file is looking in C:\TestProg\myfiles\test.txt.

If you want to find that file anywhere, then you need a recursive search algorithm, that will traverse the filesystem.

Aatch
  • 1,846
  • 10
  • 19
1

If you're deleting files in the same directory that the program is executing in, you don't need specify a path, but if it's not in the same directory that your program is running in and you're expecting the program to know what directory your file is in, that's not going to happen.

Regarding your code above: the following examples all do the same thing. Let's assume your path is /home/kim/files and that's where you executed the program.

// deletes /home/kim/files/somefile.txt
boolean result = new File("somefile.txt").delete();

// deletes /home/kim/files/somefile.txt
File f = new File("somefile.txt");
boolean result = new File(f.getCanonicalPath()).delete();

// deletes /home/kim/files/somefile.txt
String execPath = System.getProperty("user.dir");
File f = new File(execPath+"/somefile.txt");
f.delete();

In other words, you'll need to specify the path where the deletable files are located. If they are located in different and changing locations, then you'll have to implement a search of your filesystem for the file, which could take a long time if it's a big filesystem. Here's an article on how to implement that.

0

The piece of code that you provided could be compacted to this:

boolean success = new File(catName + ".txt").delete();

The success variable will be true if the deletion was successful. If you do not provide the full absolute path (e.g. C:\Temp\test for the C:\Temp\test.txt file), your program will assume that the path is relative to its current working directory - typically the directory from where it was launched.

You should either provide an absolute path, or a path relative to the current directory. Your program will not try to find the file to delete anywhere else.

thkala
  • 84,049
  • 23
  • 157
  • 201