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.
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