This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte streams  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html

CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid. Since xanadu.txt contains character data, the best approach is to use character streams, as discussed in the next section. There are also streams for more complicated data types. Byte streams should only be used for the most primitive I/O.



Why should I avoid? What problems can happened If I use Byte stream to read from text files?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably, when one reads a file, one is going to do some work with it (read values, process records, build reports, etc.). If we are reading text, we want to be working with characters, not bytes, so we use character streams so we can easily build Java String objects for processing.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks friend,

I want to know what happened in behind,

what are the details?What is difference between fileinputstream and filereader streams?What does ObjectOutputStream do that is suitable for read objects?

All the files in bytes, But there is something in these streams.

May you explain more about stream wrappers? It is said for example , char stream writes on byte streams, Is it right?

Thank you very much ineed
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone answer please?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be patient. Not everyone checks the forum every day.
Most of your questions are answered in the tutorial you were looking at. Simple streams are wrapped with streams of greater complexity. Object streams use a process called serialization to save an object's state.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is for byte stream:



This is for chat stream:



streams are pockets that cross through network.stream is a stream

I want to know what happened when we use FileWriter ,e can use stream as char?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to be very specific in our terms so we know we are talking about the same thing.

abalfazl hossein wrote:
This is for chat stream:


Character stream.

abalfazl hossein wrote:
streams are pockets that cross through network.stream is a stream


Data is sent across the network in packets. This is invisible to us at the Java API layer.

abalfazl hossein wrote:I want to know what happened when we use FileWriter ,e can use stream as char?


If it is a stream of characters, sure. If it is binary data, i.e. an image, you can lose data because the binary data can have values that will not make sense for characters.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Stream is consists of bytes anyway, We can fancy a stream like a train and its wagon. These wagons are all bytes. Because in computer we deal with 0 and 1. this is bit. and 8 bits become a byte.

char: The char data type is a single 16-bit Unicode character.


byte: The byte data type is an 8-bit



1-I want to know what happened when a JAVA program use FileWriter ,That program read stream as character?

2-Does JAVA program write something on stream?


 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Character encoding. In short, the encoding (usually the system default which can handle most characters) specifies how to turn characters into bytes and vice versa.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still don't get the answer
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know unicode and UTF-8.

But I want to know technical details about how to read stream as char. and its difference with byte stream
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone answer me please?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference in the streams itself. Bytes are being written and read to and from a file, for example. Both types of streams do this.

The difference between a simple FileInputStream and a FileReader is that they provide different methods. They both read bytes from the file, but the FileReader, after reading them, converts them to Java chars.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid. Since xanadu.txt contains character data, the best approach is to use character streams, as discussed in the next section. There are also streams for more complicated data types. Byte streams should only be used for the most primitive I/O.


May you give me examples for the most primitive I/O?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, you work with byte streams when a file has a specific format which you can work with, and you need to read or write individual bytes.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much stephan, May do a favor and give me a real example by code? When do you need to read or write individual bytes.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can't really think of an example that would be trivial yet still convey the point.

Have you ever worked with non-text files before? I use FileInputStream when I have to reverse bytes in order to convert from little endianness to big endianness.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!