• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Copying from original to temporary file and renaming temporary to original?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it why the hell temp file won't rename to original?


I didn't actually creat this temp folder it is only there to be temporarily used by Java and to be renamed back to original.





I have some utility methods to obtain file from current location in Eclipse project. All my .txt files are store in "data" package inside Eclipse project.






Now, I have some Identifiable object, which I'm changing through application, I want to record that change and to reflect that change in file. Unique identifier for each Identifiable object is actually the first value in file, that is how I know that is the object (line in file) that now need to be changed. Once I hit it, I am writing that changed object in temporary file, along with others unchanged object(lines).






I cannot figure out why temporary file won't change name to original? I'm getting temporary file in "data" package with changed object (which is I want) but there's no way to change name of the file to original value. Any help?

 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do delete first, then renameTo.

I wouldn't open the file for append. What if the renameTo failed.
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look into the "Files" utility class. It has methods delete() and move() which do what you're trying to do but will throw an exception with useful debugging information in the event of a problem.
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know.
Why are you using input streams and output streams for text files?
Why are you using legacy code with File rather than Path? That is all explained in the Java™ Tutorials.
Where are you renaming your file? You haven't shown us that code, nor told us what happens. Do you suffer exceptions or anything? Is the old file completely closed before you try to rename the new file? I think the NIO2 section of the tutorial link I posted tells you about renaming files.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote: Do delete first, then renameTo


Tried that, same result.

Carey Brown wrote: I wouldn't open the file for append. What if the renameTo failed.


Tried that, same result. How renameTo() can fail? What could possibly happen to make it fail. This thing is bugging me, mostly because in my previous project I used same FileUtils class with same functions and it was working perfectly in that project. Now..like I have water in my CPU or something...

Carey Brown wrote:You might want to look into the "Files" utility class


Campbell Ritchie wrote:Why are you using input streams and output streams for text files?  



I really need to do it this way If I get this to work, the projects is smooth sail from there...I will definitely try to learn other and probably more efficient ways you suggested, the first next chance I get. But for now I gotta do it this way unfortunately.

Campbell Ritchie wrote:Is the old file completely closed before you try to rename the new file?


Aren't the files closed with


Campbell Ritchie wrote:Where are you renaming your file? You haven't shown us that code, nor told us what happens.


No exceptions poping up, no runtime errors, nothing...Just "temporary.txt" appears in "data" package, containing edited object/line, which I so desperately want to..
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Miljan Puletic wrote:No exceptions poping up, no runtime errors, nothing...Just "temporary.txt" appears in "data" package, containing edited object/line, which I so desperately want to..

You won't get exceptions with the methods that you are using. You will get a boolean which you are currently not doing anything with. And, even with a boolean that won't tell you what went wrong. Use the methods in "Files".
 
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking back on this, here's a better approach.

1. Copy the original to the new, temporary file.
2. Rename (NOT delete) the original file to a backup name (filename.bak). If there's an existing backup file, delete IT, then rename.
3. rename the new, temporary file to live under the original filename.

You can then delete the backup file if/when it pleases you.

Also, you CANNOT rename files across volumes in most OS's. In Windows terms, that would be trying to rename T:filename.txt to E:filename.txt. In the Unix OS's, it would depend on what's mounted where.

These days, you can generally use "rename' to move a file to a new directory and/or change its name, but moving it to a new volume cannot be done by juggling directory entries, the actual data would have to be copied. And rename won't do that.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote: 1. Copy the original to the new, temporary file.
2. Rename (NOT delete) the original file to a backup name (filename.bak). If there's an existing backup file, delete IT, then rename.
3. rename the new, temporary file to live under the original filename.



Thank you Tim for replying. I tried to do what you said in this manner, but didn't get back desired result.



Any suggestions? I would really appreciate if some can figure this one out...
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Miljan Puletic wrote:. . . Aren't the files closed with
. . . . .

Not necessarily, no. That code won't be reached if an exception is thrown by the preceding code. Nor will the writer be closed if closing the reader throws an exception. That wasn't the correct way to close readers and writers even ten years ago, before Java7; the close() statements had to go in finally blocks. Since Java7, which is ten years old, the correct way to close such objects has been to use try with resources.
But that isn't your real problem. You appear to be reading and writing successfully. If anything went wrong there, you would have suffered an exception.
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something's wrong with your code and without some feedback in the form of exceptions you'll be debugging for hours.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying again.

Carey Brown wrote:




As you can see I tried few different things. Problem is that "workers.txt" (original file) is there, but when I try to delete it, whether through Files.delete() or File.delete() I get NoSuchFileException and false. What the heck? I read https://docs.oracle.com/javase/tutorial/essential/io/fileio.html but cannot get an idea why the f*** is not working.



I got this message

 
Tim Holloway
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Miljan Puletic wrote:
Thank you Tim for replying. I tried to do what you said in this manner, but didn't get back desired result.



If I wasn't so lazy, I'd look up our Wiki link for "ItDoesntWorkIsUseless".

We could do more if you posted the stack trace.

In the mean time, you keep playing around with file paths. Just create File objects and work with them directly. It's simpler. Despite its name, the java.io.File class doesn't actually directly work with files. It's more accurate to say that it's a container for a file reference. Which can be expressed as a file/directory name/path, but doesn't need to be when creating, deleting, moving or renaming files.
 
Tim Holloway
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, we overlapped. You posted the stack trace while i was writing. But is that the complete stack trace, or is there a "Caused by" secondary trace that you didn't post? We do nee the whole thing.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a secondary trace? this one is poping up on the console, once it reach line Files.move(...). I cannot use Files.move() since both temp and original file are in the same package?Should I go with creating a new empty back up package and to move there original? The problems still remians with renaming? I'll try to use move() with COPY_ATTRIBUTES parameter, maybe that will work. I'll go dig up some more. Keep you people posted...Thank you all for replaying so quickly
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Miljan Puletic wrote:I got this message

Unfortunately you cut off the critical first lines of this error message.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am not mistaken, after checking few times, this is doing the job I wanted...God, I'm gonna die



 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well are you renaming the files successfully? If so, well done .

But that code is not correct because line 29 might throw an exception, in which case line 30 won't be executed. Don't write try‑finally for closing resources. Use try‑with‑resources, as I told you earlier. It would be better not to use File at all,, but do everything in NIO2.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well are you renaming the files successfully?  


Hell yeah!

Campbell Ritchie wrote:Use try‑with‑resources, as I told you earlier.



Like this?

 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's a lot better
Don't write myString.equals("") or myString.length() == 0. Write myString.isEmpty(). Method introduced in Java6.
 
Mike Gosling
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed it. Thank you all for guiding me to the solution
 
ice is for people that are not already cool. Chill with this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!