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.

Friday, October 24, 2008

Mason tops the list of schools to watch for 2009

By Dave Andrews and Tara Laskowski Mason was named the nation’s number one university to watch on U.S. News and World Report's new list of “Up-and-Coming Schools” published on Friday, Aug. 22. The list, comprising 70 colleges and universities across the country, identifies “schools that have recently made the most promising and innovative changes in academics, faculty, students, campus or facilities.” In its annual peer assessment survey, U.S. News asked education experts to identify schools that met those criteria. This is the first time a list of up-and-coming schools has been created. "Being recognized by U.S. News & World Report for this is both an honor and validation of our efforts,” says Mason President Alan Merten. “This acknowledgment is something that every one of our faculty, staff, students and alumni should be proud of.” Celebrating its 37th anniversary in 2009, Mason has strong academic and research programs, which concentrate on producing successful students and accomplished alumni with a global focus. The university has a reputation for forward-thinking academic offerings. In the 1980s, Mason established the first engineering school in the country to focus on information technology to meet the workforce needs of an emerging high-tech economy. Mason was also the first university in the country to offer doctoral programs in conflict resolution, bioinformatics, computational social sciences, and information technology; and the first to offer a graduate degree in biodefense. In the past three years, Mason has added 27 degree programs, including advanced degrees in climate dynamics, information security, and neuroscience; as well as undergraduate degrees in conflict analysis and resolution, global affairs, global and environmental change and film and video studies. The university also partnered with the Smithsonian Institution in fall 2007 to create the Smithsonian Semester, which allows students to live on-site at the Conservation and Research Center of the Smithsonian's National Zoo and study global-scale conservation issues and civic concerns. The university boasts some of the most cutting-edge research in the biosciences, from cancer research to thwarting biological weapons. Its new biomedical research laboratory is one of 13 nationwide being built with the help of a $25 million grant from the National Institute of Allergy and Infectious Diseases, part of the National Institutes of Health. The university's students are innovators as well. From discovering new galaxies to lobbying legislators on policy issues, Mason students have success in all areas. Mason has one of the strongest undergraduate research programs, and students regularly publish research that contributes to the knowledge of their field. The school has produced acclaimed authors, popular television personalities, celebrated Olympic athletes and renowned scientists. Mason is a young university that continues to grow. The school is in the midst of its largest structural transformation ever, investing more than $500 million in construction between 2002 and 2013. Highlights include multiple new academic buildings, an on-campus hotel and conference center and what will soon be one of the largest academic, residential communities in Virginia. “For universities or colleges to achieve and maintain excellence, it is imperative that they continually seek ways to be innovative, enhance their academic programs and upgrade their facilities,” says Provost Peter Stearns. “This strategy has allowed our faculty to pursue their research, and students to achieve their educational goals.” Ranking List http://colleges.usnews.rankingsandreviews.com/college/national-uc-rank Source http://gazette.gmu.edu/articles/12396/