Win a copy of Head First Android this week in the Android forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Byte streams

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone answer please?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still don't get the answer
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone answer me please?
 
Saloon Keeper
Posts: 13391
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13391
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13391
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic