Sometimes, I want to use an ObjectOutputStream to write something to a file or sending a little image over the network. But BufferedImage and many other classes don’t implement java.io.Serializable and then the Stream cancels writing. Is there a way avoid that?
Thanks, Martijn
— ObjectOutputSteam docs
However, you could avoid all this by using one of the classes in
javax.imageio. Specifically theImageIO.write(RenderedImage, String, OutputStream)method, becauseBufferedImageimplementsRenderedImage. You can then read it back out withImageIO.read(InputStream), which returns aBufferedImage.You’ll probably want a different
OutputSteamtype, though. In addition to the normalOutputStreams, there are several specialImageOutputStreams.Edit: I missed this before:
To get a list of valid strings for the middle argument, you can call
ImageIO.getWriterFormatNames()