Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with delete a file  RSS feed

 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I have a problem with delete a file.

Sample code:

File f=new File(source);
for(int i=0;i<=file.length;i++)
file[i].delete();

This code is run perfectly when i testing a simple program.
but in my project this code is not able to delete the files,it
always returns false. I closed all streams which i used but it not deleting a files.Please Help Me..

Thanks in advance
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abiram kumar:


Sample code:

File f=new File(source);
for(int i=0;i<=file.length;i++)
file[i].delete();



What is the environment for you project? Here is what I faced a few months back , might be useful to you.

My code to delete was running fine in the development environment (Windows 2000) , but was failing in production environment (UNIX).

The reason is the source.When I trimmed this it worked file in UNIX also.FileSystem implementation are different for different filesystems and file are closely related to those.I think in case of a UNIX file , it takes the source as source and if the source string has some spaces in the end , then the File object that you have created might not be a valid descriptor to a file in the filesystem.
In case of NT filesystem implementation they might be taking care of that.
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply rahul,My project development is runing in windows xp.Even i trim source string,not deleting the file.Still boolean value is returns false.
Please help me...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then some other live descriptor might be there for that file or the user id with which JVM is running might not have enough permission to delete the file.
 
chennai Kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave all permission to directory,but i run simple program(Separate program)it deleting all files properly.but i Implement in project delete can not work properly.
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Rahul. This problem is usually caused by an open stream. I'd put more debugging in your code. Print out the file location so you know you are trying to delete the file you think you are deleting. Check File.exists() and the return value of File.delete to narrow down what's going wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!