Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to print a representation of an ArrayList of strings to the given output stream (out):

public void outputBag(OutputStream out)
        throws IOException
{


}

I'd usually do this in the main function by using writer and be done with it. But I have to use the above method this time. Thing is, I have no idea how OutputStream is supposed to work, I spent a couple of hours reading javadocs and watching videos but to no avail.

Can you guys show me an example of how I'd do this?

Thank you.

share

1 Answer

Java's streams, readers, and writers are modular and designed to work together. Usually you wrap bare OutputStreams in something with a richer interface, like a PrintStream, to make it easier to use. So, for example:

PrintStream ps = new PrintStream(out);
ps.print("The array list has this many elements: ");
ps.println(theArrayList.size());
ps.println(theArrayList.toString());
ps.flush();

I don't know what your "string representation of an ArrayList" should be, so I just made something up; you may want to do something different, but it would be similar in spirit.

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.