This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Deleting file cannot be done

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deleting orders.txt cannot be done and thus the renaming of the new file (temp.txt)  that is going to replace the original file could not be done.



 
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
               
The calls to flush() are unnecessary because the calls to  close() takes care of that. Line 47 should fail because newFile no longer exists because you've renamed it. You don't need 'dump', you already have 'oldFile'.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:Deleting orders.txt cannot be done and thus the renaming of the new file (temp.txt)  that is going to replace the original file could not be done.

Do you get any error messages? If so, post the complete error message.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 16: Why do you open tempFile for appending? If you're luck, tempFile is non-existent or empty, but if it is still there with some text in it it will cause you grief.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to use the Scanner's default delimiter. The delimiter you have will fail on Windows machines.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 36 should call println(), not print().

Why do you need arrays? You could use the variables without making them arrays.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "try with resources" to guarantee the the resources are closed properly. Of course this would mean moving the delete and rename steps outside of the try/catch block.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Use "try with resources" to guarantee the the resources are closed properly. Of course this would mean moving the delete and rename steps outside of the try/catch block.



Everything can be edited and the results show positive, the only thing I could not to is replace the old file with the new file, nothing to do with the appending steps.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 16: Why do you open tempFile for appending? If you're luck, tempFile is non-existent or empty, but if it is still there with some text in it it will cause you grief.



I opened the tempFile to copy the existing data that I do not want to modify from the original file and then print in the data that is updated into it. Old file > Scan for data > ID matches oID > replace that line with new data > temp file is now updated with correct data > delete old txt file > rename the new file into the name of the old text file.

Anyway as I said, the appending stuff works just fine. However no luck in deleting the oldfile and replacing it with the new file.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to work as you expected.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This seems to work as you expected.



It's not working at my end. Any ideas?
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:It's not working at my end. Any ideas?

This does not give anyone enough to go on. What happens? What were you giving it as input? What did you get as output? What were you expecting?
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Felix Ho wrote:It's not working at my end. Any ideas?

This does not give anyone enough to go on. What happens? What were you giving it as input? What did you get as output? What were you expecting?



These are my outputs after running the program. I was expecting the temp.txt to be renamed into  orders.txt. As you can see the temp.txt file is modified at order 001 from pending to completed, but the orders.txt file remains untouched.
The program I'm trying to write basically just scans for whichever ID/Code matches the input and changes the status from pending to completed.

The problem that I face now has nothing to do with the input and appending of the files but rather the inability of the program to successfully replace the old file with the new file.

Thank you for your patience and guidance. I am new here, forgive me.

//temp.txt
001 C001 Completed
002 C002 Pending
003 C003 Pending

//orders.txt
001 C001 Pending
002 C002 Pending
003 C003 Pending
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Do you have either file open in another program when you run this code?



No , I do not.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



I think the problem here is that the orders.txt file is opened somewhere and it cannot be deleted. I could not solve that part of the program, I believe if the old file (orders.txt) can be deleted the temp.txt can be successfully be renamed into orders.txt.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Please post (using code tags) the contents of "orders.txt" and "temp.txt".




 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



this is the ouput after adding the code.

Order accepted!Renaming: C:\Users\User\Documents\NetBeansProjects\POS System\temp.txt --> C:\Users\User\Documents\NetBeansProjects\POS System\orders.txt
ERROR: delete failed: C:\Users\User\Documents\NetBeansProjects\POS System\orders.txt
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm at a bit of a loss here. "It works for me" )

Have you tried deleting all (if there's more than one) temp.txt files and then running it?

Add the debugging code I just posted.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm at a bit of a loss here. "It works for me" )

Have you tried deleting all (if there's more than one) temp.txt files and then running it?

Add the debugging code I just posted.



Yes I certainly have tried deleting all the temp files and then running it again, the temp file is fine. The only issue is I cant delete or replace the orders.txt file.

Were you able to delete and replace the orders file on your end?
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What operating system are you on?



Windows 10
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will tell you why delete failed.

 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm at a bit of a loss here. "It works for me" )

Have you tried deleting all (if there's more than one) temp.txt files and then running it?

Add the debugging code I just posted.



Is the orders.txt file protected from being modified by any chance? I checked the properties of the file, everything is allowed from deleting to writing to reading.
I can't wrap my head around why it is not possible to delete the file.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This will tell you why delete failed.



Output:

java.nio.file.FileSystemException: orders.txt: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1126)
at pos.system.staffLogin.acceptOrder(staffLogin.java:189)
at pos.system.staffLogin.main(staffLogin.java:90)
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:

Carey Brown wrote:This will tell you why delete failed.



Is there a way to close the orders.txt file before replacing it?

 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:Were you able to delete and replace the orders file on your end?


Yes.

I wonder if NetBeans has this file open for some reason (?).
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:Is there a way to close the orders.txt file before replacing it?


Try-with-resources performs the close operation.
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make sure we're in sync, could you post the code as you currently have it?

Is there perhaps another place in your application where you open the file and don't close it?
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my full code for my current module. I do not have any other instances running the text file anywhere.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Felix Ho wrote:

This is my full code for my current module. I do not have any other instances running the text file anywhere.



Ouput for this full code :

 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Remove lines 182 & 183



Ok I’ll try that out when I wake up tomorrow, it’s currently 3am where I’m at and I need to go to sleep. Thank you for your guidance and I might need your help again.
Bless you.
 
Felix Ho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Remove lines 182 & 183



This fixed the problem I was facing, thank you so much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!