Easy question: from InputStream to File (FileOutputStream)

Hi,
   Can anyone help with this problem:

I have an InputStream coming over from HttpClient (from an HTTP GET request), and wish to write the data coming in from the InputStream object to a file (say, "raw.xml"); I can't seem to find any information online on how to connect InputStream to FileOutputStream or something similar...

Any help will be appreciated!
NGInterfaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
method.getResponseAsStream()

and copy it
0
CEHJCommented:
Sorry, it's

method.getResponseBodyAsStream()
0
CEHJCommented:
You could also do

FileOutputStream out = new FileOutputStream("x.html");
out.write(method.getResponseBody());
out.close();
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

NGInterfaceAuthor Commented:
Oh, I mean, I already got method.getResponseBodyAsStream() -- I just don't know how to turn that into a file...the amount of information coming over HTTP is very large, so responseBody() may not be the right approach...
0
CEHJCommented:
FileOutputStream out = new FileOutputStream("x.html");
final int BUFFER_SIZE = 1 << 10 << 3; //8KiB buffer
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
InputStream in = method.getResponseBodyAsStream();
while ((bytesRead = in.read(buffer)) > -1) {
      out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
> InputStream in = method.getResponseBodyAsStream();

better off using a buffered stream

InputStream in = new BufferedInputStream(method.getResponseBodyAsStream());
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.