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?