reading a random access file

Posted by user1067332 on Stack Overflow See other posts from Stack Overflow or by user1067332
Published on 2012-11-25T23:02:10Z Indexed on 2012/11/25 23:03 UTC
Read the original article Hit count: 170

Filed under:
|
|
|

The file I create has text in it, but when i try to read it, the output is null.

here is the file the creates the text file, it creates the file and puts it in the right postion

import java.util.*;

import java.io.*;
public class CreateCustomerFile
{
   public static void main(String[] args) throws IOException
   {
      int pos;
      RandomAccessFile it = 
         new RandomAccessFile("CustomerFile.txt", "rw");
      Scanner input = new Scanner(System.in);

      int id;
      String name;
      int zip;
      final int RECORDSIZE = 100;
      final int NUMRECORDS = 1000;
      final int STOP = 99;
      try
      {
         for(int x = 0; x < NUMRECORDS; ++x)
         {
            it.writeInt(0);
            it.writeUTF("");
            it.writeInt(0);
         }
      }

      catch(IOException e)
      {
         System.out.println("Error: " + e.getMessage());
      }
      finally
      {
         it.close();
      }
      it = 
        new RandomAccessFile("CustomerFile.txt","rw");
      try
      {
         System.out.print("Enter ID number" +  
                        " or " + STOP + " to quit ");

         id = input.nextInt();
         while(id != STOP)
         {
            input.nextLine();
            System.out.print("Enter last name");
            name = input.nextLine();
            System.out.print("Enter zipcode ");
            zip = input.nextInt();
            pos = id - 1;
            it.seek(pos * RECORDSIZE);
            it.writeInt(id);
                it.writeUTF(name);
            it.writeInt(zip);
            System.out.print("Enter ID number"  +
               " or " + STOP + " to quit ");
            id = input.nextInt();
         }
      }
      catch(IOException e)
      {
         System.out.println("Error: " + e.getMessage());
      }
      finally
      {
         it.close();
      }
   }
}

Here is the file to read, the pos is correct but ouput always goes to the error: null.

import javax.swing.*;
import java.io.*;
public class CustomerItemOrder
{
   public static void main(String[] args) throws IOException
   {
      int pos;
      RandomAccessFile it = 
         new RandomAccessFile("CustomerFile.txt","rw");
      String inputString;
      int id, zip;
      String name;

      final int RECORDSIZE = 100;
      final int STOP = 99;
      inputString = JOptionPane.showInputDialog(null,
         "Enter id or "+ STOP + " to quit");
      id = Integer.parseInt(inputString);
      try
      {
         while(id != STOP)
         {
            pos = id -1 ;
            it.seek(pos * RECORDSIZE );
            id = it.readInt();
            name = it.readLine();
           zip = it.readInt();
            JOptionPane.showMessageDialog(null, "ID:" + id +
               " last name is " + name + " and zipcode is " + zip);
            inputString = JOptionPane.showInputDialog(null,
               "Enter id or " + STOP + " to quit");
            id = Integer.parseInt(inputString);
         }
      }
      catch(IOException e)
      {
         System.out.println("Error: " + e.getMessage());
      }
      finally
      {
         it.close();
      }
   }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about readfile