File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ObjectInputStream

 
sreek, Thiru
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read a file and skip some bytes ... using "skipBytes" method, but it always skips zero bytes irrespective of what the argument is....
here is the code.
long fp...(from some other method)
fos = new FileInputStream("emp.ser");
bos = new BufferedInputStream(fos);
oos = new ObjectInputStream(fos);
try{
int avail = oos.available();
System.out.println("avail = " + avail);
int b = oos.skipBytes( (int)fp);
if(b != (int)fp )
System.out.println("b =" + b + " & fp = " + fp);
}
catch( Exception e) { e.printStackTrace(); }

output is :
avail = 0
b=0 & fp = given val
I am striving with this from past 10 to 12 hrs ... someone please help me.
 
Jim Baiter
Ranch Hand
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the API doc for skipBytes() in the DataInputStream class which the ObjectInputStream is using.
"Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes. However, it may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned."
What you should probably do is just loop calling readByte() in each iteration for the number of bytes you want to skip or you could create a byte[] of that length and call readFully().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic