Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting path of a file from absolute path string

 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What actually creates this file in the first place?
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know what could be the reason but this simple program worked as intented...





Maki Jav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic