Large Files
byte array could be as long as int. Also read and write functions can read or write no longer than the size of byte in one read/write. So does it mean that files bigger than 32767 bytes can't be read or written with one .read() or .write() . I am sure I am at wrong track. But, I am confused because File length() function returns long but byte couldn't be initialized lile byte [] b = new byte [f.length()] because f returns long. So what do I do if my files are huge. My files are around 32-64 MB long. How do i read them ? do I need to loop through read/write ?
There seems to be alot of confusion in your post starting with the difference between bytes, shorts and ints. In Java the maximum (in theory) size of an array is 2,147,483,647 elements. Which means if that is a copy buffer of a file then the maximum file you can hold the content of in one array is ~2GB.
But I am little concerned when you say one read/write. You are using a BufferedStream right?
And last generally speaking you do not want to open a 64MB file all at once. Use a buffer, read a chunk of the file, process, read the next chunk, process, etc.
> I actually thought that size of int is 32767 and
> didn't realize that it's the size of int that's much
> larger.
4 bytes.
-2^31..2^31-1
> I am talking about bufferedstreams and I
> would have to read the entire contents because I need
> to write it to a server on socket .
No, you don't need to read it all at once. Read a chunk from the file, write it to the socket. Repeat until done.
> What's more
> efficient ?
Don't worry about that right now. Worry about making it work. Then if it's too slow, make it faster.
Reading and writing a byte at a time with no buffering would be slow. As long as you use a buffered streams/readers/writers with a decent buffer size (anywhere from, say, 1k to 32k, as an initial ballpark).
jverda at 2007-7-29 14:43:33 >
