Results 1 to 4 of 4

Thread: Random access files?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    9

    Random access files?

    i was reading how to use random access files, but it says each field should be of the same length inorder for the seek() to be used. but we cannot keep the fields of fixed length, say names and addresses will be of different length for different records....and i wanted to use the random, but this is confusing?

    also what i want is
    like i have three fields, first is a serial number, second name and third is say address, how do i make this fixed lenght?
    also when i will view this..i want to view them by serial number like when user puts 2 the record number 2 should show, like all the three fields of record 2. please help....
    Last edited by jframe; Jul 24th, 2003 at 05:42 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I don't think that you would be able to use seek() unless the fields were of a fixed length in bytes. Each character is encoded as Unicode which is two bytes so going from character to character would be easy using readByte() but word to word would be impossible as all the fields would be different lengths.

    For instance the following code would print out each character that is contained in the file to the console.
    Code:
    import java.io.*;
     
    public class R{
     public static void main(String[] args){
       
      File f;
      RandomAccessFile raf = null;  
     
      try{
      f = new File("C:" + File.separator + "U.txt");
      raf = new RandomAccessFile(f,"r");
      }catch(IOException e){;}  
    
      char filechar = 0; 
     
      for(;;){
       try{
        System.out.print(filechar = (char) raf.readByte()); 
       }catch(IOException e){
          System.exit(0); 
       }
      }
     }
    }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You could use a StringTokenizer if you wanted to keep the fields on the same line in the file as the size of the field would have no effect since you are just tokenizing the words that are delimited by spaces.
    Code:
     import java.io.*;
     import java.util.*; 
     
     public class R{
      public static void main(String[] args){
       
       File f;
       RandomAccessFile raf = null;  
       String s = null;
      
      try{
       f = new File("C:" + File.separator + "U.txt");
       raf = new RandomAccessFile(f,"r");
       s = raf.readLine();
      }catch(IOException e){;}  
    
      StringTokenizer st = new StringTokenizer(s);
    
      while(st.hasMoreTokens()){  
       System.out.print(st.nextToken());
       System.out.println(); 
       }
      }
     }

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width