Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

properties file editing

 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to pass in a key value, and then delete a line inside properties file based on the key. Somehow I can't find any methods to do this in Properties class. Does anyone know how to?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the property does not exist in your map of Properties it will not be saved to a file. So:

should do it.
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
tried using the following code:

inside the properties there are a few keys and "test" is one of them, if I set fout to true, it will append the whole properties file below without the removed key. But if I don't set fout to true, all the keys and values wil be removed.
Actually I want the original file to be updated. Is it I need to use a workaround, say, store it in a different fout file after removing the key, and then delete the fin file, then rename the fout file to the fin file?
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside the properties there are a few keys and "test" is one of them, if I set fout to true, it will append the whole properties file below without the removed key. But if I don't set fout to true, all the keys and values wil be removed.


What do you mean by "set fout to true"? fout is an outputstream, not a boolean variable.


BTW, the properties store() method, just writes the basic properties out to the stream. If the original property file had an ordering, comments, and extra line spaces for readability, they will all be gone. In other words, if you store(), don't expect the property file to be nicely formatted anymore.

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would strongly advise you don't try to have an InputStream and and OutputStream for the same file at the same time. Depending on the operating system, it may throw an error, or you may get some very strange results. Keep these actions separate: open an input stream, read it, and close it. Then open an output stream, write to it, and close it.

I assume by "set fout to true" you're talking about the FileOutputStream constructor that takes a booolean, for appending. You don't want to append here; you want to write a new file.
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry, Tim is right about the boolean for fout, I mean the append argument for the constructor, sorry about the lack of info.

Thanks guys, I think I'll use a workaround to get what I want.
[ December 22, 2006: Message edited by: vivien siu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic