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();
}
}
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.