• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

File I/O Stream OR Object I/O Stream?

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering since FileInputStream and FileOutputStream have constructors that take file object, to simplify/streamline ones design,

1. can File I/O Streams handle the job when Object I/O Streams are typically used?
2. if not, is there some sort of rule of thumb as to when one must use the Object I/O Streams, and not File I/O Streams?

Thanks
 
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it depends on what you are doing.

ObjectOutputStream/ObjectInputStream are for primarily for serializing/deserializing objects when you want to persist objects to/from local storage, or when transferring objects between applications/platform through a network.

FileOutputStream/FileInputStream is for working with Files to write/read raw bytes.
 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java™ Tutorials?
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have. You just like to get on my case, don't you?


But Ron had it pegged. I was hoping that maybe there was a way that I could design an application that didn't have to have so many different types of Streams, since it has to both handle raw bytes and persist objects with local storage involved and on top of that transferring them (both raw bytes and objects) to remote storages.

Anyways, back to the tranches.
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is called the decorator patter‍n, but I might be mistaken. The whole idea of the design of the java.io package is a response to the fact that there are all sorts of different sources of information, and when you get there, you find that the information is in all sorts of different formats. So each class is intended to work with a particular source with information in a particular format. The different formats are handled by different classes at the back end.
The same sort of thing applies to outputs.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think it is called the decorator patter‍n, but I might be mistaken



As usual you are not mistaken. I looked into decorator design pattern and some examples online, and changing the design from abstract's IS-A to decorator pattern's HAS-A, hence modifying each I/O component behavior, saved me a lot of back and forth and much closer to the so-called streamlining the overall code that I was looking to do.

Thanks a bunch.
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . As usual you are not mistaken. . . .

I make lots of mistakes.

Thanks a bunch.

That's a pleasure If you find a copy of Core Java II by Cay Horstmann or by Cay Horstmann and Gary Cornell, the IO chapter shows some of the combinations of classes you can use to read and write files etc. There were major changes in Java7 (2009).
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you find a copy of Core Java II by Cay Horstmann or by Cay Horstmann and Gary Cornell, the IO chapter



Found it. I'll definitely be picking through chapter 2.

Thanks again.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic