Java – How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

You should use Java’s built in serialization mechanism. To use it, you need to do the following: Declare the Club class as implementing Serializable: public class Club implements Serializable { … } This tells the JVM that the class can be serialized to a stream. You don’t have to implement any method, since this is … Read more

What is the use case for null(Input/Output)Stream API in Java?

Sometimes you want to have a parameter of InputStream type, but also to be able to choose not to feed your code with any data. In tests it’s probably easier to mock it but in production you may choose to bind null input instead of scattering your code with ifs and flags. compare: class ComposableReprinter … Read more

How can I convert an InputStream to a DataHandler?

An implementation of the answer from Kathy Van Stone: At first, create a helper class, which creates a DataSource from an InputStream: public class InputStreamDataSource implements DataSource { private InputStream inputStream; public InputStreamDataSource(InputStream inputStream) { this.inputStream = inputStream; } @Override public InputStream getInputStream() throws IOException { return inputStream; } @Override public OutputStream getOutputStream() throws IOException … Read more

Log4J: How do I redirect an OutputStream or Writer to logger’s writer(s)?

My suggestion is, why dont you write your OutputStream then?! I was about to write one for you, but I found this good example on the net, check it out! LogOutputStream.java /* * Jacareto Copyright (c) 2002-2005 * Applied Computer Science Research Group, Darmstadt University of * Technology, Institute of Mathematics & Computer Science, * … Read more

How to put the content of a ByteBuffer into an OutputStream?

Look at Channels.newChannel(OutputStream). It will give you a channel given an OutputStream. With the WritableByteChannel adapter you can provide the ByteBuffer which will write it to the OutputStream. public void writeBuffer(ByteBuffer buffer, OutputStream stream) { WritableByteChannel channel = Channels.newChannel(stream); channel.write(buffer); } This should do the trick!