What is the easy way to write to OutputStream from Java to InputStream?

Asked 2 years ago, Updated 2 years ago, 99 views

Is there an easy way to write OutputStream with InputStream? It's not hard to write to the byte buffer code, but I think I'm missing an easier way...

If InputStream has in and OutputStream has out

byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
    out.write(buffer, 0, len);
    len = in.read(buffer);
}

Is there any easier code than this?

java io stream

2022-09-22 22:25

1 Answers

The Apache Common library has a class called IOUtils. You can do it easily by using the method called copy in this class.

InputStream in;
OutputStream out;
IOUtils.copy(in,out);
in.close();
out.close();

You can do it like this.


2022-09-22 22:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.