XML Data Files In this post I will show you how to read and write data types in simple xml data files using XML DOM. DOM means Document Object Model. We are mainly concerned here with reading and writing some data of the same types we have been working with in previous articles. So i.e. xml data files <some document> <some element> <some data name=value/> <some data name="value"/> <some data> some text </some data> </some element> </some document> XML Documents XML document can be more complex with other parts to parse. In this, we have a document root which has as children which have children which have attributes which contain the data we want to store. But also text data can be stored if we use <some data> text </some data> not...
Reading and Writing Objects to files or streams is fairly straight forward. Here is a working example. There are a few things to know. Use serialver.exe to create a serial id number for the class. A class must implement Serializable interface and should use @serial tag in Javadoc comments. A cryptic coded file is created which is not very editable or readable. In a later article I will talk about serializing objects to xml streams and files.
This article demonstrates using a fixed field width | delimited text file similar to a spread sheet. You can store and retrieve various data types in give row,col cells. It expands columns if new data is longer than the cell. It does not add new columns or rows however and if you try to access columns or rows that are no there you get out of bounds errors. In the example I show a survey note book sheet that we use in mapping caves. A future version of this might calculate variables that would be used in cave mapping and add them to the sheet. This could also be used for mapping things above ground.
I show you how to read and write your own property file which contains name=value pairs. Java comes with a Properties object to do this exact thing and I will write an article about that object later. Or expand this article to include an example. This article is another example of using a delimiter and in this case it is = symbol. Later I will upgrade this to include new line as delimiter to handle long strings.
Simple examples for writing and reading to and from a text file line by line. Write, Read and Append. In Java we not only use FileWriter and FileReader but also BufferedWriter and BufferedReader to speed up reading and writing in just about all cases.