Results 1 to 4 of 4

Thread: RandomAccessFile? [resolved]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    RandomAccessFile? [resolved]

    Anyone know how i can open a file for RandomAccess and increment the file pointer from character to character? I open the file in "r" read mode but im not sure if this is the right way to position the file pointer.

    It makes sense since each character should be two bytes even though they are encoded in UTF-8 since they fall within the ASCII range. But all i keep getting is ? marks

    Code:
      import java.io.*; 
      
      public class X{
       public static void main(String[] args){
       try {
       File f = new File("C:\\Java\\Brandon.txt");
       int counter = 0;   
       RandomAccessFile raf = new RandomAccessFile(f, "r");
       
      while(true){
        System.out.println(raf.readChar());
        raf.seek(counter += 2);
      }
          
     }catch(IOException e){}
       }
     }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Here's another example. I have a file
    that just contains my name "Brandon".
    Im simply trapping a key from the console
    and comparing it with the first character
    within the file. "B". But when i press B the
    it dosent match the first character in the file.
    I get System.out.println(B + " Was pressed" +
    " but it does not match " + ?);
    Code:
     import java.io.*; 
     
     public class X{
      public static void main(String[] args){
     try {
       File f = new File("C:\\Java\\Brandon.txt");
       int counter = 0;   
       RandomAccessFile raf = new RandomAccessFile(f, "r");
      
      
       int i = System.in.read();
       char c = raf.readChar();
       char pressed = (char) i;
       raf.seek(counter += 2);  
        
       System.out.println(pressed + " Was pressed" + " but it does not match " + c); 
       
      }catch(IOException e){}
       }
     }

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Anybody? Here's my latest attempt at this problem....
    The file contains 7 letters and i get 7 question marks.


    Code:
    import java.io.*; 
     
     public class X{
    
      public static void main(String[] args){
     try {
       File f = new File("C:\\Java\\Brandon.txt");
       int i = 0;   
       RandomAccessFile raf = new RandomAccessFile(f, "r");
       
     
       long charcount = getFileCharCount(f); 
    
      while(i < charcount){
       System.out.print(raf.readChar());
       raf.seek(i);
        i++;
      }
     
      
      }catch(IOException e){}
     }
    
    public static int getFileCharCount(File file) {
    
        String lineOnebyOne = null;
        String finalText = null;
        int numberOfChar = 0;
      try{
        BufferedReader br = new BufferedReader(new FileReader(file));
        while ((lineOnebyOne = br.readLine ())!= null){
          finalText += lineOnebyOne;
           numberOfChar = finalText.length();
        }
          br.close();
         }catch(IOException e){System.err.println(e + "getFileCharCount");}
         
           return numberOfChar;
       }
     }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Using char filechar = (char) r.readByte(); works.
    Using char filechar = r.readChar(); dosen't.
    Go figure.

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