|
-
Jul 4th, 2002, 07:20 PM
#1
Thread Starter
Dazed Member
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.
Last edited by Dilenger4; Sep 2nd, 2002 at 01:26 AM.
-
Jul 5th, 2002, 10:27 PM
#2
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.
-
Jul 9th, 2002, 01:56 PM
#3
Thread Starter
Dazed Member
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);
}
}
-
Jul 9th, 2002, 05:44 PM
#4
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.
-
Jul 10th, 2002, 12:07 AM
#5
Thread Starter
Dazed Member
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);}
}
}
Last edited by Dilenger4; Jul 10th, 2002 at 11:08 PM.
-
Jul 10th, 2002, 04:51 PM
#6
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?".
-
Sep 2nd, 2002, 01:25 AM
#7
Thread Starter
Dazed Member
Just wanted to say thanks. Now i can finally resolve is post.
-
Sep 4th, 2002, 11:54 PM
#8
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|