Results 1 to 8 of 8

Thread: bi-directional files? [resolved]

  1. #1

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

    Question bi-directional files? [resolved]

    Im trying to position a file pointer from a RandomAccessFile using the public void seek(long pos ) method but an IOException stating a Negative seek offset keeps getting thrown. I should be able to go back a specific amout of bytes right? I know of other ways i could do this but i wanted to use a RandomAccessFile.

  2. #2
    VirtuallyVB
    Guest

    Thumbs up

    It looks like pos should be from 0 to length-1 in bytes. Are you trying to set a negative number for pos?

    public void seek(long pos)
    throws IOException
    Throws:
    IOException - if pos is less than 0 or if an I/O error occurs.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I created a quick app so i can track the file pointer to see where it is but for some reason the getFilePointer() method from the java.io.RandomAccess class is giving me problems. I should be getting a pointer position displayed in the second JTextField but instead nothing is displayed.
    Code:
    import java.io.*; 
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class X {
       public static void main(String[] args){
          new Y(); 
       }
     }
    
     class Y {
        public Y(){
      JFrame jf = new JFrame("Brandon");
      JPanel jp1 = new JPanel();
      JPanel jp2 = new JPanel(); 
      JTextArea jta = new JTextArea(1,25); 
      JTextArea jta2 = new JTextArea(1,25);
      
      Container c = jf.getContentPane();  
      jta.addKeyListener(new Z(jta2));
    
      jf.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
           System.exit(0); 
        }
      });
         
      jp1.add(jta);
      jp2.add(jta2);  
      c.add(jp1, BorderLayout.NORTH);
      c.add(jp2, BorderLayout.SOUTH); 
      jf.setSize(300,300);
      jf.setVisible(true);
     }
    
     public static RandomAccessFile getRandom() throws IOException{
       RandomAccessFile r = new RandomAccessFile("C:\\Java\\Novice1.txt","r");
          return r; 
      }
     }
    
    class Z extends KeyAdapter { 
      
       private RandomAccessFile r;
       private JTextArea jta2; 
     
      public Z(JTextArea jta2){
       try{
         this.jta2 = jta2; 
         r = Y.getRandom();
        }catch(IOException e){System.err.println(e);}
       }
    
     public void keyListener(KeyEvent ke) throws IOException{
      
         long pointer = r.getFilePointer();
         String p = String.valueOf(pointer);  
         jta2.append(p);
      }
    }

  4. #4
    VirtuallyVB
    Guest

    Thumbs up

    What are you asking your new class Z (which is already a KeyAdapter) to do with the method
    public void keyListener(KeyEvent ke) ?

    Does a KeyAdapter have the method "keyListener"? KeyListener is an interface and keyListener is a new method you are adding (you are not overriding an existing method).

    Change your Z method block
    public void keyListener(KeyEvent ke) throws IOException{...

    to
    Code:
      public void keyTyped(KeyEvent ke){
       try{
         long pointer = r.getFilePointer();
         String p = String.valueOf(pointer);  
         jta2.append(p);
       }catch(IOException ioe){
         JOptionPane.showMessageDialog(null,   "Z Error:\n" + ioe, "Z.keyTyped(KeyEvent ke)", JOptionPane.ERROR_MESSAGE);
       }
      }
    and typing into the northern textarea will cause the southern textarea to show that the filepointer is at position 0.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    ok much better. I forgot that the method within the KeyAdapter class is keyTyped(). Im still trying to shake the rust off.

    I slapped this togther real quick and it works pretty good. Now at least i can see where the file pointer is and when a java.io.IOException Negative seek offset is being thrown.
    Code:
     import java.io.*; 
     import java.awt.*;
     import javax.swing.*;
     import java.awt.event.*;
    
    public class X {
       public static void main(String[] args){
          new Y(); 
       }
     }
    
     class Y {
       public Y(){
        JFrame jf = new JFrame("Brandon");
         JPanel jp1 = new JPanel();
          JPanel jp2 = new JPanel();
           JPanel jp3 = new JPanel();  
            JTextArea jta = new JTextArea(1,50); 
             JTextArea jta2 = new JTextArea(1,50);
              JTextArea jta3 = new JTextArea(25,50);
      
      Container c = jf.getContentPane();  
        jta.addKeyListener(new Z(jta2,jta3));
          jf.addWindowListener(new WindowAdapter(){
       
      public void windowClosing(WindowEvent e){
         System.exit(0); 
      }
     });
         
      jp1.add(jta);
       jp2.add(jta2);
        jp3.add(jta3);  
         c.add(jp1, BorderLayout.NORTH);
          c.add(jp2, BorderLayout.SOUTH); 
           c.add(jp3, BorderLayout.CENTER); 
            jf.setSize(600,300);
             jf.setVisible(true);
      }
    
     public static RandomAccessFile getRandom() throws IOException{
       RandomAccessFile r = new RandomAccessFile("C:\\Java\\Novice1.txt","r");
        return r; 
      }
     }
    
    class Z extends KeyAdapter { 
       private RandomAccessFile r;
        private JTextArea jta2; 
         private JTextArea jta3; 
          private long pointer;
     
      public Z(JTextArea jta2, JTextArea jta3){
       try{
        this.jta3 = jta3;     
         this.jta2 = jta2; 
          r = Y.getRandom();
           }catch(IOException e){System.err.println(e);}
         }
    
     public void keyTyped(KeyEvent ke){
      try{
      char keytyped = ke.getKeyChar();
      if(keytyped == 8){
          r.seek(pointer - 1);
      }
        Character c;
         char filechar; 
          pointer = r.getFilePointer();
           String p = String.valueOf(pointer);  
            jta2.append(p);
             filechar = (char) r.readByte();
              c = new Character(filechar);
               jta3.append(c.toString());
                }catch(IOException e){System.err.println(e);}
               }
              }

  6. #6
    VirtuallyVB
    Guest
    Interesting code. It reminds me of a customized "notepad.exe" that for whatever the person types, it displays something clever instead of what is being typed.

    You'll get a handle on those listeners. The thing that got me in the past was, "Which listener or method should I use?".

  7. #7

  8. #8
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Thumbs up You're Welcome

    I'm reborn from the ashes and getting PHP help from a young jedi. These forums sure come in handy.

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