| 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!!
|
 |
 |
|
|
subject: how to reset the pointer to begining again after read through the file?
|
|
|