• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting path of a file from absolute path string

 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What actually creates this file in the first place?
 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know what could be the reason but this simple program worked as intented...





Maki Jav
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic