read

M

misiek

Hello!
I`ve got problem.
I tried to read data from file using FileInputStream and 'read' method...
I don`t exactly know how to do it.
I have to use 4096 bytes blocks.
sorry for my english

Thanks
 
M

misiek

According to
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
you just call read() with a byte array, and whatever the size of your
array, that's how many bytes you'll read, though you have to check
return status

Hey!
I found this example but it just doesn`t work...I can`t see last part of the
file I`m reading

byte [] bufor = new byte [4096];
int bytesRead = 0;
int offset = 0;

while(offset < dlugosc)
{
//bytesRead = is.read(bufor,offset,bufor.length - offset);
bytesRead = is.read(bufor);
if(bytesRead == -1) break;
offset += bytesRead;
System.out.println(new String(bufor));
}
is.close();
 
M

misiek

misiek said:
According to
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
you just call read() with a byte array, and whatever the size of your
array, that's how many bytes you'll read, though you have to check
return status

Hey!
I found this example but it just doesn`t work...I can`t see last part of
the file I`m reading

byte [] bufor = new byte [4096];
int bytesRead = 0;
int offset = 0;

while(offset < dlugosc)
{
//bytesRead = is.read(bufor,offset,bufor.length - offset);
bytesRead = is.read(bufor);
if(bytesRead == -1) break;
offset += bytesRead;
System.out.println(new String(bufor));
}
is.close();

dlugosc = File.length() ;)
 
T

Thomas Weidenfeller

According to
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html#read(byte[])
you just call read() with a byte array, and whatever the size of your
array, that's how many bytes you'll read,

Not at all. You are just guaranteed that you will not read more bytes
than what fits in the array. You can get less. You can even get zero.
though you have to check
return status

The return value is not just a status value. It tells you how many bytes
you indeed got. Use this value if you further process the array data,
never just the size of the array.

/Thomas
 
T

Thomas Weidenfeller

misiek said:
dlugosc = File.length() ;)

(a) Using File.length() is a bad idea. File sizes can and do change
while you read. E.g. all it takes is that someone deletes the file
between the moment you called File.length() and while you use the data.

(b) Using binary I/O when you want to do text I/O is a rather bad idea, too.

(c) Converting the buffer to a string without taking the just read
'bytesRead' size into account is a bad idea. The array might only be
half filled, but the String() constructor doesn't know about this.

(d) Doing the conversion (which should not be done, see (b)), without
specifying a charset is a bad idea. Java will use the platform's default
which might or might not the the right one.


Please work through Sun's I/O tutorial at

http://java.sun.com/docs/books/tutorial/essential/io/index.html

Oh, and beginners questions are best asked in comp.lang.java.help

/Thomas
 
R

Roedy Green

I tried to read data from file using FileInputStream and 'read' method...
I don`t exactly know how to do it.
I have to use 4096 bytes blocks.

see http://mindprod.com/applets/fileio.html

Just tell it what you want to and it will write the code for you.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top