• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

ObjectInputStream

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic