Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing in chunks  RSS feed

 
Suma Chandran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to copy the contents of a file (size 6 kb)to another file. This is the program that I have written and it is works fine.

import java.io.*;

public class FileRead
{
public static void main(String args[]) throws Exception
{

InputStream istr = new FileInputStream("C:\\myfile.txt");
OutputStream ostr = new FileOutputStream("C:\\myfileout.txt");
byte[] buffer= new byte[64000];
int len;

while ((len = is.read(buf)) > 0 )
os.write(buffer, 0, len);

istr.close();
ostr.close();
}
}

I would like the contents to be written in chunks of 1kb. It reads 1kb from input file , and writes it to output file. Then it reads the next 1 kb from input file and writes it to output file, etc.

As of now I have set the buffer size to 64kb, and hence it reads a file of size 6kb in one go.

How do I set the buffer size to 1kb and write to the output file in chunks of 1 kb?

thanks.
Suma.
 
Paul Clapham
Sheriff
Posts: 22470
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would come close. But what's the reason for this requirement? Why does it matter whether you send 1 byte at a time or a million bytes?
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
would come close. But what's the reason for this requirement? Why does it matter whether you send 1 byte at a time or a million bytes?

Performance. Selecting the correct buffer size is very tricky business especially when you have to eak out maximum possible performance.
 
Suma Chandran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a small example which I thought I could work on. Incase I need to send a file of 1 MB, I need to probably divide it into chunks and send it through - maybe 10kb chunks.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul had it right. The only thing you need to change is your buffer size.
There is a tradeoff between buffer size and transfer speed and determining the "best" size is going to depend on a number of factors. Java Platform Performance contains some instructive examples and metrics. I usually just use a BufferedInputStream(8k buffer in JDK 1.5).
 
Suma Chandran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for helping.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!