File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Getting path of a file from absolute path string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Getting path of a file from absolute path string" Watch "Getting path of a file from absolute path string" New topic
Author

Getting path of a file from absolute path string

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hello,

I have:

String path = "c:\\myfolder\\sampleFile.txt"; //This is mandatory, the complete path to file comes from elsewhere in the program

I want to delete this file after modifying its contents (storing the modified contents to a different file at same location: c:\\myfolder\\sampleFile.txt.tmp).

I want to delete the original sampleFile.txt file, and then rename the temp sampleFile.txt.tmp file to sampleFile.txt.

I am doing something like:




Its not working!!!

How do I do this?

Thank you


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Rajdeep Biswas wrote:Hello,

I have:

String path = "c:\\myfolder\\sampleFile.txt"; //This is mandatory, the complete path to file comes from elsewhere in the program

I want to delete this file after modifying its contents (storing the modified contents to a different file at same location: c:\\myfolder\\sampleFile.txt.tmp).

I want to delete the original sampleFile.txt file, and then rename the temp sampleFile.txt.tmp file to sampleFile.txt.

I am doing something like:




Its not working!!!

How do I do this?

Thank you


Don't you think you appended a .tmp to the path name (so that c:\\myfolder\\sampleFile.txt becomes c:\\myfolder\\sampleFile.txt.tmp ) which does not exists. Thus , trying to open this file should result in an error


Programming is about thinking, NOT coding
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Don't you think you appended a .tmp to the path name (so that c:\\myfolder\\sampleFile.txt becomes c:\\myfolder\\sampleFile.txt.tmp ) which does not exists. Thus , trying to open this file should result in an error


Hi,

I am deleting the originalFile, it has path and not tempPath.

The tempPath file is what I am renaming later.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Do you read the original file; if yes you should ensure all streams are closed. In the code you mentioned a comment "perform modifications in originalFile data and write them to tempFile." - if you do this by code, you can also show it here.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

The data is like:

1|alcatel|25|10|passwordsdsd
2|cisco|35|10|passwordsdsd
3|huawei|15|10|passwordewferwtert

The first data is SP ID, and it is unique across file.

After asking user input for SP ID, the program will verify and then try to locate. If located then writes the complete original data in a tmp file with modified line.

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Although reading, recognizing the right line and writing to a temporary file is working,
the problem lies in deleting the original file, and then renaming the temporary file to the original one.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
This sounds a lot like another thread I participated in recently - although the details are different, most of my ideas and suggestions are the same as in this thread. From your comments about your print statements it seems pretty definite that no error is thrown before the delete, and the delete is executing, but returning false. So I would check to see if there are any other programs accessing the file on your system. Can you delete the file manually, or does the system say it's being used? If you're sure nothing else is using it, then try adding a Thread.sleep() for a few seconds before the delete(), to see if it makes a difference.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hi,

I have done some modifications:

Added a finally block:



Even I tried to set and then do
Is there any more better way to debug so that I can understand the root cause of why the file is not getting deleted!

[Java version is 1.6, Win XP SP3, MyEclipse 7.0]
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Okay, the lines written were not faltering. Either JVM or/and OS or/and other no-under-my-control-considering-coding conditions are responsible for non-deletion.....
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2286
    
  49
What actually creates this file in the first place?
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Tony Docherty wrote:What actually creates this file in the first place?

When data is entered for the first time, the file is created. And then with subsequent addition of information, data is appended to the same file.
So later, we can access, modify the information from those files.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2286
    
  49
Ok, so your app is creating and updating the file. That means, assuming you haven't opened the file in another app, that your application must be locking the file and preventing it from being deleted. As previous people have already said you need to make sure you close every stream that has been opened during creating and updating this file before you try to delete it.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Yes Tony.
Rajdeep wrote: Okay, the lines written were not faltering. Either JVM or/and OS or/and other no-under-my-control-considering-coding conditions are responsible for non-deletion.....

Because twice out of several tries, the working was correct, but in remaining cases, the files was not getting deleted, failing the rename method to rename the tmp file to the original one (which should have been deleted)....so gave up. Program was flawless, the permissions were correct, stream were closed properly, still!!! ewwww!!!
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Don't know what could be the reason but this simple program worked as intented...





Maki Jav


Help gets you when you need it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting path of a file from absolute path string