• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to reset the pointer to begining again after read through the file?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried fileInputStream.reset(), it throws exception says the "mark/reset not supported".

I need to read the file into a byte array, first I have to use a while loop
while ( (currentByte = fin.read() ) != -1) {
fileLength ++; }

to count how many bytes of the file, then using the counter to new a byte array, then use
fin.read(array,0,array.length);
but it doesn't work properly, the file(a pdf) cannot be open. But if I hard code the lengh of the file without a loop, the file generated properly.
So I guess it is the file pointer problem, after the while loop, the pointer is positioned at the end of the file, thus even the subsequent call I specify the beginning position, still doesn't work properly.

I am using websphere 5.1, just support java 1.3, 1.4 has some new features I can use to avoid a while loop.

And I don't want to create another file input stream, looks not good.

Anybody can help? Thanks a lot.......
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to run through the file to get the size. You can either use java.io.File to get the size or just read the file into a java.io.ByteArrayOutputStream, which grows as data is added.
 
philber fang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a Joe Ess. I try your way and it works!!

I am supprised that I got the answer so quickly!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic