Search This Blog

15 February 2010

Reading of text files

In more or less every medium sized or bigger project there will be a need for reading text files sooner or later. Most of the time the text files will be quite good organized either as fixed post files or separated file (comma, tab and newline being the most common separator).

If the file is of type fixed post file one of the best method is to match the file structure into a class structure in the program and read line by line an instance of such an object. This supports multiple instances of subsections for elements and such too. Serializing the file becomes quite easy and goes farily smooth and if there is some error in the filecontent it will propagate and show early instead of being hidden away. Also it is possible to store information from the file in a database farily easy with this approch since a direct mappping to a database structure is possible. If you are using NHibernate (or Hibernate for Java) it is even possible to create a mapping file where the underlying class is the same for the database as for the textfile. One more advantage is the possibility to create automatic tests for the objects use to verify that the business rules are followed in the application.

With a file that is using some sort of separators to differentiate between the different post many times the easies way is to read up the entire file (or row by row) and run a split over the separators and then parse each element into a underlying object that holds the structure of the file. Using this approch measn its easy to detect errors in length of each element in the file as well as each field for each element. This also means that if the file becomes corrupt or changed in the future (not uncommon) this will be detected and can be handled correctly.

When it omes to have to actually read the text from the files it differes from project to project as well as from language to language but most of the times some simple loop expression is easy enough to use and quite effective at the same time. Don't complicate things, keep it easy and you will earn stability and readability of the source code.

No comments:

Post a Comment