• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple text file records using properties or ?

 
Bob Cernohous
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have need for a very simple text records. A few different types of records, a dozen records total in the file. One example, using text field=value formatting:

name=bob count=2 data=test timestamp=2005/04/04...
name=joe count=2 data=test timestamp=2005/04/04...
deleted=tom timestamp=...

Any suggestions? I hate the idea of manually parsing it and dealing with whitespace, line continuation, long fields, null fields, etc. I thought csv but seaching these forums decided that wasn't so easy either. I thought XML but that seems like overkill.

Then I thought properties and this seems to work, but I'm wondering if anyone has other suggestions.

What I did conceptually is reformat the records (of course these are one pair to a line in the real file):

bob=name bob.count=2 bob.data=test bob.timestamp=2005/04/04...
joe=name joe.count=2 joe.data=test joe.timestamp=2005/04/04...
tom=deleted tom.timestamp=...

To delete a record, I guess I need to create a whole new Property without that record and store that to the file. So, during processing I would set the values of deleted records to "". Then I would enumerate the properties and only copy non-"" properties and write the new properties. Seems to work in this simple test (code doesn't match the sample record above but it's pretty easy):

[CODE]

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class TestProperties
{
TestProperties()
{
Properties prop = new Properties();
prop.setProperty("bob.one", "test");
prop.setProperty("bob.two", "test2");
prop.setProperty("bob.three", "test3");
prop.setProperty("joe.one", "test");
prop.setProperty("joe.two", "test2");
prop.setProperty("joe.three", "test3");
prop.setProperty("tom.one", "");
prop.setProperty("tom.two", "");
prop.setProperty("tom.three", "");
try
{
prop.store(
new FileOutputStream("c:\\$user\\test\\file.properties"),
"master");
prop.store(
new FileOutputStream("c:\\$user\\test\\file1.properties"),
"active");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void test1()
{
try
{
Properties originalProperties = new Properties();
originalProperties.load(
new FileInputStream("c:\\$user\\test\\file1.properties"));
originalProperties.setProperty("joe.one", "");
originalProperties.setProperty("joe.two", "");
originalProperties.setProperty("joe.three", "");
Properties replacementProperties = new Properties();
for (Enumeration e = originalProperties.propertyNames();
e.hasMoreElements();
)
{
String key = (String) e.nextElement();
String value = originalProperties.getProperty(key);
if (!value.equals(""))
{
replacementProperties.setProperty(key, value);
}
}
replacementProperties.store(
new FileOutputStream("c:\\$user\\test\\file1.properties"),
"active replacement");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public static void main(String[] args)
{
TestProperties tp = new TestProperties();
tp.test1();

}
}

[\CODE]

Output to file.properties is (what kind of sorting is it doing?):

#master
#Mon Apr 04 14:10:30 CDT 2005
bob.one=test
joe.one=test
tom.two=
tom.three=
bob.three=test3
bob.two=test2
joe.two=test2
joe.three=test3
tom.one=

Output to file1.properties is:

#active replacement
#Mon Apr 04 14:10:30 CDT 2005
bob.two=test2
bob.three=test3
bob.one=test

Not the nicest looking text records but functional. And deleting a record is a little painful but since I don't expect too many records, enumerating it isn't so bad.

But, like I said, any better ideas?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the Java API Documentation you'll see that java.util.Properties extends java.util.Hashtable so you do have a remove method. You just have to remove all the entries you'd derive for a particular name.
Question: what do you do for a duplicate name? You may want to create a unique id (i.e. sequential number) and add that to each property (i.e. name.1=joe name.2=bob. . .) Also, using numbers rather than number-words will make it easier to load multiple records for someone (i.e. joe's tests: test.1.0=x test.1.1=y test.1.2=z). Don't forget that characters are sequential types in Java. Enumerations (added in JDK 1.5) may help too.
I just used Properties to create a simple web photo book. Each directory (i.e. page of photos) under the main has a properties file where the page name, page display order, and picture captions are stored. Works like a charm. It should work for you as long as you can munge the names and associated properties together.
I've been intrigued by using RandomAccessFile to build a simple database, but never past the prototype stage. If you have a small data set you could probably get away with serializing a Hashmap/Hashtable to file and loading it when you need it.
There's other simple in-process databases like
Berkeley DB, but they may be overkill for your purposes.
[ April 04, 2005: Message edited by: Joe Ess ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used to do dozens of little control files to configure command line utilities, so I'm always comfortable making my own little files. I'd be very tempted to just make tag=value lines.

Parsing it is simple, too ...

This is all simple and fun, but non-standards-based and may confuse the next person who has to read the code. For their benefit you might want to bite the bullet and use properties or csv or something standard.

As far as updating, I'd read the whole file into memory, update in memory, and rewrite the whole thing. That's just me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!