aspose file tools
The moose likes Java in General and the fly likes how to reset the pointer to begining again after read through the file? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to reset the pointer to begining again after read through the file?" Watch "how to reset the pointer to begining again after read through the file?" New topic
Author

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

philber fang
Greenhorn

Joined: Dec 30, 2004
Posts: 12
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.......
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8264

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
philber fang
Greenhorn

Joined: Dec 30, 2004
Posts: 12
Thanks a Joe Ess. I try your way and it works!!

I am supprised that I got the answer so quickly!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to reset the pointer to begining again after read through the file?
 
Similar Threads
using the reset() method in InputStream Class
trouble with a file
PrintStream & PrintWriter
downloading & writting file >> caching
175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release