aspose file tools*
The moose likes Beginning Java and the fly likes properties file editing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "properties file editing" Watch "properties file editing" New topic
Author

properties file editing

vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
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?


I'm not available, my BF's name is WORK.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If the property does not exist in your map of Properties it will not be saved to a file. So:

should do it.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
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
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: properties file editing