Popular Posts

Monday, October 27, 2008

Java’s File Handling Mechanism

In general there are two types of activities that a user can perform on a file, he can read or write something to that file. In both cases he needs to have appropriate access permissions on that file. Some days ago I was completing an assignment in which I have to save some records in the file using java language. The basic task was to create a user account which includes his name and telephone number and then stores it in the text file then later on if he wants to delete his record, delete it and finally prints all the records of the text file. Summarizing the above

  1. Create a user account (including his name and number)
  2. Deleting a user account (by name or by telephone number)
  3. Print records.

You can perform above all three tasks very easily by using java collection arrays provided if you don't have to save these records for a long time. There are two approaches to this problem and I will discuss both of them in this article. Approach one is mine which I have used it for solving this problem which is not a good solution. Approach two is better than the first one and unfortunately I figured it out after submitting my assignment.

Approach One (not recommended):

I took the both the user name and password save it in the file after proper input validation. Everything went well until the user the wants to delete a record from the file which introduces the java file handling limitations.

  1. Deleting a record from file requires the file to be opened in append mode in my scenario.
  2. The first file handler has to be closed first (as I am already writing records to the file it is already opened in the write mode access, so I need to close it before continuing).
  3. In append mode the file pointer will always start writing to the end of file by default which works good as if you are adding more records to the file but not in deleting where the records exists anywhere in the file.
  4. Once you found the matching record you can't just simply delete it, you can over write it some other string (like replace the account name with "Deleted" and later on while printing just don't display the records with deleted account name.(creepy way but still works)
  5. Here's how I did it. I matched the user name with my records list and if it matches with my records list means user account exists in database then just save it in a another text file. Later on while printing all the records if the user account name exits in both the text files simply ignore it and don't display it on the screen. (works good for fewer number of records in both the text files)

Pros/cons of my approach: Once I opens the text file in write mode I have to close it before switching it to the another option like deleting or showing records and once I close then I am not able to open it again from the last point. If just opened it in a append mode then it will throws an exception of file not previously formed. So that's why I came up with this approach of having a text file one is the master one with all entries and the other I will create it in deleting mode in which I will save only those entries which the user wants to delete it so for the showing I will just compare these two file contents printing only non duplicate records. In append mode I can only move the file pointer to the opening and closing file only which doesn't help me if the desired deleting record exists in the middle of the file.

Approach Two (recommend):

Take both the username and telephone numbers from the users and after validating the input just add it to string. Java handles string very intelligently and it is easier to append strings in java just by using a "+" plus sign between two strings will concatenate both the strings. The user can anytime perform any function and as the records are still in the memory it will be much faster than fetching the record from the file. While existing the program you can simply save this whole one string of records in the file. You should consider some identifiers and place it in your string which will later on make it easy for record searching like in this example you can place an "=" (equal) sign between the account name and its telephone number and also you can place a ":" (colon) sign at the end of the string to mark the end of the record in the string. These token signs are helpful in searching for the specific records in the string. While searching for account name you know now that the account name starts with the index of that alphabet and it will ends with the index of = (equal) sign while telephone number starts with the index of = (equal) sign and goes until : (colon) sign. So deleting a record not a problem anymore as know you have only string and if that contains the deleting value just split the string until the index of matching string alphabet with the help of java substring function and split the original string once more on the matching string : (colon) sign (remember : marks the end of the string). Here is an example code for deleting a record provided you have both the user account name and telephone

String temp2 = user account name + "=" + telephone number + ":"; // creating the exact record format

String temp3 = original string.replace(temp2, "");// replacing it with null values

Original string = temp3;// swapping the values of original string with the new temporary original string

Now below is the example code in which you have only one value it can be a user account name or user telephone number but the below code is for user account name you have to play with to make it work for telephone number too. It is not hard just replace some values and you are done with it.

String temp2 = original string.substring(original string.indexOf(account name)); //splitting the string until account name. Original string part before the marked account name.

String temp1 = temp2.substring(temp2.indexOf(":")); //extracting the marked account name along with the telephone number.

String temp3 = original string.replace(temp2, temp1); // excluding the marked values in the original string.

Original string = temp3; // swapping the values of original string with the new temporary original string.

I hope I have explained everything correctly up to my knowledge and you have enjoyed reading it. Please let me know if you didn't understand any part and I will be happy to assist you. Let me know your feedbacks on this article.

Post a Comment