Saturday, January 25, 2014

A simple android java IO class using Random File Access

       Just a little background on why I created this IO class. I began creating a project, an android app, as my collage project. I found little help online on how to read and write an  array containing strings to a file, with most advice on sites like stack overflow stating that I should read the contents of the file into an array and then manipulate the data and write back to file. This seemed like an inefficient, inelegant and non scalable solution. I stumbled across the Random File Access class which seemed perfect. So I wrote and developed a simple class which allowed easy reading writing and deletion of records, which is reasonably efficient. To make my life easier I structured my code in a modular manor so that the functions are not excessively interdependent and can be easily modified. 


   How it works
        The code is simple and readable (including comments) so any details I leave out can be found there. 

Write To file:
      All entries (each string stored in the array) have a fixed size which is defined in the class itself. All entries are padded to this length with #'s  at the end of the strings. It is your responsibility to do validation to prevent more than the fixed size being written to file which would cause an error. After data has been padded, the entries in the array are written to file. 

Read From File: 
       Currently when reading from file the method reads all entries stored in a file and returns an Array of Array (ArrayList<ArrayList<String>>). This could be modified to incorporate a specific search function using the code found in the delete file method. This may be added in the future. All  #'s are removed from the strings and the Array of Arrays before being returned. 

Delete Entry: 
      This allows the deletion of files, using the first entry of the array that you have written as a search parameter. In my project a used the timestamp of the time of writing the array as the first element in the array so that it would be both unique and searchable. If you do use the same identifier on multiple entries then the first one in the file will be deleted only. The entry is deleted by filling writing over the entire entry with #'s. This allows the write file method to overwrite any gaps in the file, which ensure efficient use of data storage.  

Limitations of the system:
  • One of the major limitations is that every entry must be below the maximum size that is fixed at the beginning of the class. To make it specific sizes for each entry would require customisation to the code. 
  • Another limitation is that the file size will not decrease when entries at the end of the file are deleted, even though gaps in the file are filled when writing to the file. This could be easily fixed by checking if the last entry in the file is empty (a #). This is usually not a problem if you app will only use small and similar amounts of data at any one time. 
  • As the system searches each record in a serial manor, it may not be suitable for large amounts of data in terms of speed.
Examples usage:


Write to file:
ArrayList<String> DataArray = new ArrayList<String>(2); //2 needs to be set as number of entries so that the array is the correct size.
File file = File file = getBaseContext().getFileStreamPath("Homework.dat");
DataArray.set(0, "HelloWorld");  //index 0 of the array
DataArray.set(1, "GoodBye"); //index 1 in the array
WriteToFile(DataArray, file, 2); //Pass the array and 2 entires are contained
//HelloWorld and GoodBye are now written to file.

Read from file:
ArrayList<ArrayList<String>> DataArray;
File file = File file = getBaseContext().getFileStreamPath("Homework.dat");
DataArray = FileIO.ReadFromFile(file, 2) //file is of type File, 2 is for number entries in each //record. Entries should be the same value as specified when writing to file.
//DataArray.get(0).get(0) would be HelloWorld
//DataArray.get(0).get(1) would be GoodBye

Delete Entry:
File file = getBaseContext().getFileStreamPath("Homework.dat");
String Search = "HelloWorld";
DeleteEntry(file, Search, 2); 
//This will delete the entry from file which includes HelloWorld and GoodBye strings.

Source Code:
       You can get the source code here https://drive.google.com/file/d/0B0AvU0Nd226bMFIwY3dUaEZNcEU/edit?usp=sharing  and any questions you may have feel free to ask below.. The Code is released under the MIT licence which is quite non restrictive and allows you to include this code in closed source solutions with out the need to contact me. If you do create a cool project I would love to hear about though and if you make any improvements I would appreciate if you send the code, but it is your choice. Do comment if you found this useful and good luck.

https://drive.google.com/file/d/0B0AvU0Nd226bMFIwY3dUaEZNcEU/edit?usp=sharing