1

I want to copy data from demo1.txt to demo2.txt, although I can do it by BufferedReader, I want to copy by BufferedInputStream / BufferedOutputStream. Please show me how to do this.

import java.io.*;
class stream4
{
    public static void main(String arr[])
    {
        BufferedInputStream bfis=new BufferedInputStream(new FileInputStream("demo1.txt"));
        BufferedOutputSteam bfos=new BufferedOutputStream(new FileOutputStream("demo2.txt"));
        byte b[]=(bfis.read());
        bfos.write(b);
        bfis.close();
        bfos.close();
    }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • [`read()`](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#read()) only returns "the next byte of data, or -1 if the end of the stream is reached". You need a loop which reads one byte and then writes one byte until all bytes are read from the input. – jmiserez Aug 12 '14 at 17:36
  • You can't just make your code up. The read() method doesn't return a byte array. Look it up. – user207421 Aug 12 '14 at 17:58

3 Answers3

2

change

byte b[]=(bfis.read());

to

    byte[] b = new byte[1024];
    try {
        for (int readNum; (readNum = bfis.read(b)) != -1;) {
            bfos.write(b, 0, readNum);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    finally {
        bfis.close();
        bfos.close();
    }

as bfis.read() the next byte of data, or -1 if the end of the stream is reached.

SparkOn
  • 8,806
  • 4
  • 29
  • 34
2
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main
{
    public static void main(String[] args) throws Exception
    {
       String fromFileName = "demo1.txt";
       String toFileName = "demo2.txt";
       BufferedInputStream in = new BufferedInputStream(new FileInputStream(fromFileName));
       BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(toFileName));
       byte[] buff = new byte[32 * 1024]; 
       int len = 0;
       while ((len = in.read(buff)) > 0) //If necessary readLine()
         out.write(buff, 0, len);
       in.close();
       out.close();
     }
}

This will do the job :). Just specify what kind of byte size you are looking at, and from there use a loop to continue to read the file.

Adam
  • 2,422
  • 18
  • 29
  • 1
    It doesn't matter what the byte array size is as long as it is greater than zero. This code works with 1, and 8192 is sufficient for most purposes. – user207421 Aug 12 '14 at 17:57
  • No, that's why there's a loop. Have a look at how the code works. It just keeps reading into the buffer and writing that many bytes from it. It doesn't care how big the buffer is. Try it with new byte[1]. In this case, as there are buffered streams both in and out, it wouldn't even be slow. – user207421 Aug 12 '14 at 18:02
0

As others correctly suggested you need your own buffer actually to read and write by portions, which is represented as byte array of the specified size. So this now make no sense in wrapping your FileInputStream and FileOutputStream with BufferedInputStream and BufferedOutputStream - they are useful if you input from stream and output by smaller portions. I suggest just making your buffer bigger than suggested (say, 16384 or 32768) and remove unnecessary BufferedInputStream and BufferedOutputStream in this case.

VirtualVAT
  • 61
  • 1
  • 5