0

I try to delete a PDF File but its not working. when I try to delete a simple text is working test.txt . Is there any other problem?

Source Code:

try{
        File f1=new File("D:\\test.pdf");       
        if(f1.delete()){
            System.out.println(f1.getName() + " is deleted!");
        }else{
            System.out.println("Delete operation is failed.");
        }
        }catch (Exception e) {
            // TODO: handle exception
        }
Karthik
  • 747
  • 4
  • 21
  • 48
Adriano_jvma
  • 455
  • 2
  • 11
  • 20

2 Answers2

0

Are you reading/writing from file in the program? If you are reading/writing using stream and don't close the stream then also it will not get deleted.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • No,file is just open before the application is run – Adriano_jvma Jun 13 '13 at 12:39
  • If the file is open then the application will not be able to delete it. It would be good if you share the complete code of this file processing. That will help us identify the root cause. – Darshan Mehta Jun 13 '13 at 14:38
-1

The file may be in use by other application(s). Also you can use deleteOnExit() in place of delete()

Mubin
  • 4,192
  • 3
  • 26
  • 45
  • @ZouZou I didn't had the privileges (enough points) to add a comment to the question at that time. Otherwise, I would have done that – Mubin Jun 13 '13 at 12:33
  • @Mubin You need at least 50 rep to comment. 57-10+2*2 = 51. So you had the privileges to comment. Anyway, that's not so important. – Alexis C. Jun 13 '13 at 12:35
  • When I answered this question, I had 41 points. Received 10 points later for upvotes. – Mubin Jun 13 '13 at 12:51