Jump to content

Okay is someone have any good reference for java stream classes help me out here. I was learning  about the different stream classes and how to use them for file input and output and i am a bit confused about the relation between the different classes and their derived classes. So if anyone have any good reference for this pls share.

I am trying to understand how the different stream classes work and which classes are linked to which classes. And also how is the outputstreamwriter and inputstreamreader different from the other character stream classes and how they work?

 

Also if i use a byte stream class to write some data to a .txt file will that be considered writing in bytes rather than characters? cause when i used byte stream class i couldnot open the txt file normally to see what was written in it.

 

Help me out here guys

Link to post
Share on other sites

some are child classes, but many are actually just wrapper around one, this design principle is known as the decorator pattern 

 

InputStreamReader for example takes in an inputstream object in its constuctors(wrapping around it) rather than subclass the inputsteam. InputSteamReader can itself be wrap inside a BufferedReader object.  Generally, the ones deeper inside the layer are consider "lower level". You can use them directly without passing them into a higher up wrapper but you will need to write a lot more code yourself to make it work. 

Sudo make me a sandwich 

Link to post
Share on other sites

If you want to know about relations between framework classes, you can look them up in the official Java documentation:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/InputStream.html

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/OutputStream.html

 

See "Direct Known Subclasses"

 

Classes derived from InputStream can be used to read data from various I/O sources (e.g. file or network), for example FileInputStream is intended to read from a file. Conversely output streams can be used to write data, so e.g. FileOutputStream is used to write to a file.

 

The various reader and writer classes then provide convenience methods on top of a streams. For example to read text from a file you'd first create a stream, then hand that to a reader that provides methods like "read line" and so on.

 

Readers or writers with "Buffered" in their name buffer input/output to increase performance. For example reading a file byte by byte is not very efficient. The buffered reader will instead read e.g. an 8 kB block of the file and then satisfy calls to "readByte" from its internal buffer, before reading the next chunk.

 

5 hours ago, Souranil21 chakraborty said:

Also if i use a byte stream class to write some data to a .txt file will that be considered writing in bytes rather than characters? cause when i used byte stream class i couldnot open the txt file normally to see what was written in it.

When you're dealing with raw bytes you need to take the text encoding into consideration. For a text file in ASCII format, every byte is one character. But if the file contains UTF-8 encoded text, a character may consist of multiple bytes (1–4 bytes). You can absolutely read/write text files using a byte stream, but you'll need to take care of the encoding/decoding yourself in this case, if you want to display the text somewhere.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×