Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file Deletion

 
Kalyan Saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File del=new File(dirName);
del.delete();

this code doesn't seem to be working.. dirName what should be passed pathname and the file of only the directory path? please suggest...

Kalyan
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to coderanch...

Pass path of the file name which you want to delete.
And make sure you have imported java.io.*;

Search google before asking any kind of question.
 
Kalyan Saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried earlier, these suggestion you have given. And searched Google also.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that File.delete returns a boolean that indicates whether the operation was successful; you should always check that and act on it appropriately.
 
Kalyan Saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.delete() is returning false every time.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And File.exists()?
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And File.exists() returns true? If that's the case then it could be a permissions problem, or the file could be in use by some other process.
 
Kalyan Saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes may be it is the tomcat.exe which is using the file.
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, it goes without saying that you should not delete any files that are part of Tomcat, or one of the web apps it runs.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if it's a directory you will need to remove all files and sub directories in it first.

Also, if you have opened an InputStream / OutputStream / Reader / Writer to the file and haven't closed it (which you should do in a finally block, or preferably in Java 7 using try-with-resources) that will also prevent the file from being deleted.
 
Kalyan Saha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to delete those files because it is saved in the database as a binary format. Every time any view is made for the image files creates a .JPG file in that folder. So, these are being a dump amplifying application size.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic