Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Can I use a BufferedOutputStream with an ObjectOutputStream to serialize an object?

0
Posted

Can I use a BufferedOutputStream with an ObjectOutputStream to serialize an object?

0

Location: http://www.jguru.com/faq/view.jsp?EID=5295 Created: Jan 17, 2000 Modified: 2000-03-13 21:50:27.046 Author: Tim Rohaly (http://www.jguru.com/guru/viewbio.jsp?EID=10) Yes. In fact, it is recommended that you buffer all your input and output unless you have a good reason not to. Here’s an example of how to do this: … String myobject = new String(“myobject”); FileOutputStream file = new FileOutputStream(“myobject.ser”); BufferedOutputStream bout = new BufferedOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(myobject); out.flush(); … Java allows you to chain I/O streams together. Using a BufferedOutputStream makes output much more efficient because the I/O overhead is then expended on large chunks of data, rather than on individual bytes. Likewise, many times it makes sense to compress your output, say if you are writing many large objects to a file or a socket. You can do this using a ZipOutputStream or a GZIPOutputStream in place of

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123