ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

File IO and Sequential Access with Java

Updated on March 18, 2010


This tutorial demonstrates basic techniques for manipulating sequential files and performing file IO in Java. A code segment is provided that opens a sequential file, reads data from it, and stores that data in an array of Java String data items.

Sample Data File

The sequential file that is read by the code should be in this format:
Note that the file is one 'record', with no line feeds or carriage returns. A sequential file has no intrinsic record structure, but it is typically delimited by a CR/LF at the end of each line of text. Our file is one line. The program could be easily modified to handle multiple lines by adding a loop.

Our logic uses the powerful split() method to parse the sequential file into individual names. Since we know the record is delimited by commas, we can "split" on commas into an array of String data items. One call to split() does it all. The commas are thrown away and we are left with individual Strings that are not quite 'clean' because they still have quotation marks delimiting them. We use a loop to cycle through all the elements in the String array and remove the quotation marks. The removal process is accomplished by calling the replace() method. Instead of replacing the quotation marks with actual characters, which would get us nowhere, we replace the quotation marks with empty strings. Each string is shortened by two characters; these characters were not strictly part of the data anyway.

 * Read the list of names from the text file into one big string.
 * Split the list of names into an array of strings.
 * Strip out the quotation marks around each name.
 * @param debug if true, provide debugging output during processing.
 * @return The number of names that were processed.
private static int ReadNames(boolean debug) {
   int count = 0, i;
   String tmp;
   private static String nameString[];             // An Array of Strings.
   try {
     BufferedReader in = new BufferedReader(new FileReader("names.txt"));
//   Read all the data from then file up to the next CR/LF
//   In this particular file, that's everything; the data is one long line.
     tmp = in.readLine();
     nameString = tmp.split(",");   // Split by the commas. Sweet.
     if (debug) {
         System.out.print(nameString.length + " names read.");
     for (i = 0; i < nameString.length; i++) {
//      Replace all the double quotes with nothing.
//      The double quotes are superfluous to us; they might be useful for
//       other programmers who process this file in a different way.
        nameString[i] = nameString[i].replace("\"", "");
   } catch (IOException ex) {
        System.out.println("ReadNames: " + ex.getMessage());
   return count;

Sequential file access and IO with Java

Sequential file access and IO with Java
Sequential file access and IO with Java

Suggested Improvents

1. Improve the error handling. The method currently contains a try/catch block that traps file IO errors that might occur when processing the sequential file. Unfortunately the catch block only prints the error message to stdout; a better strategy would be to log the error to a more permanent location so the developer can refer to it after the fact.

2. Modify the processing loop to handle sequential files that contain multiple records. The method currently handles only one record; the logic attempts to read the sequential file one time, then continues processing that single record. File IO typically requires the capability to deal with multiple records in a single file.


    0 of 8192 characters used
    Post Comment

    • profile image 7 years ago

      can't believe I'm the first one to comment. Thanks Nicomp, I love reading this stuff and putting my brain back together!

    • nicomp profile image

      nicomp really 7 years ago from Ohio, USA Thank you for commenting. My code is sorely unappreciated; :(

    Click to Rate This Article