• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ByteStreams and CharacterStreams

 
Ugly Redneck
Posts: 1006
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the following two programs..
Program 1
---------
class Test
{ public static void main(String args[])
{ byte b[] = {65,66,67,68,69}; //declaring a byte array
System.out.println(b); //printing using byte stream
}
}
Output : 65 66 67 68 69
Program 2
---------
class Test2
{ public static void main(String args[])
{ FileOutputStream fout = new FileOutputStream("test.txt");
byte b[] = {65,66,67,68,69};
for (int i =0; i<5; i++) //writing the contents of the byte
fout.write(b[i]); // array
fout.close();
}
}

At the dos prompt : type test.txt
Output :
A B C D E
Now my question is, both System.out and FileOutputStream are byte streams.. but how come the outputs are so different?? Why doesnt the second program also store the values as bytes??
Thanks in advance.
Sriraj Rajaram
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sriraj Rajaram:
Take a look at the following two programs..
Program 1
---------
class Test
{ public static void main(String args[])
{ byte b[] = {65,66,67,68,69}; //declaring a byte array
System.out.println(b); //printing using byte stream
}
}
Output : 65 66 67 68 69
Program 2
---------
class Test2
{ public static void main(String args[])
{ FileOutputStream fout = new FileOutputStream("test.txt");
byte b[] = {65,66,67,68,69};
for (int i =0; i<5; i++) //writing the contents of the byte
fout.write(b[i]); // array
fout.close();
}
}

At the dos prompt : type test.txt
Output :
A B C D E
Now my question is, both System.out and FileOutputStream are byte streams.. but how come the outputs are so different?? Why doesnt the second program also store the values as bytes??
Thanks in advance.
Sriraj Rajaram


FileOutputStream and PrintStream (System.out) are both subclasses of OutputStream and therefore they both implement public abstract void write(int b), but that is about it for the similarity.(to be more accurate PrintStream doesn't implement write it rather uses implementation of it superclass FilterOutputStream ) The "output" you are talking about is not different at all. I suspect that the ABCDE output is caused by the way the viewer you are using to display the "test.txt" file (ASCII)is presenting it (65 in ASCII is A and so on). As you know java.io package is going to be on the exam (objectives) and API doc
is pretty good source of information about Java streams. Good luck!
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sriraj Rajaram:
Take a look at the following two programs..
Program 1
---------
class Test
{ public static void main(String args[])
{ byte b[] = {65,66,67,68,69}; //declaring a byte array
System.out.println(b); //printing using byte stream
}
}
Output : 65 66 67 68 69
Program 2
---------
class Test2
{ public static void main(String args[])
{ FileOutputStream fout = new FileOutputStream("test.txt");
byte b[] = {65,66,67,68,69};
for (int i =0; i<5; i++) //writing the contents of the byte
fout.write(b[i]); // array
fout.close();
}
}

At the dos prompt : type test.txt
Output :
A B C D E
Now my question is, both System.out and FileOutputStream are byte streams.. but how come the outputs are so different?? Why doesnt the second program also store the values as bytes??
Thanks in advance.
Sriraj Rajaram


I think the second program (FileOutputStream) is storing the data in bytes, but the "Type" command you are invoking at the DOS prompt is interpreting the bytes as ASCII characters.
Savithri
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sriraj,
byte b[] = {65,66,67,68,69}; //declaring a byte array
//Here I dont thinnk you are declaring any output stream.All you are doing is declaring an array whose elements are of the primitive type byte. So
System.out.println(b); //printing using byte stream
This will just behave as normal, that is, it will invoke the toString() of your array object and print the values it contains,[b] as primitives .
FileOutputStream fout = new FileOutputStream("test.txt");
//This now constructs an outputStream, which is byte oriented.Do not confuse byte as a data type (representing integers in the range -128 to 127) and byte as a form of representing serialised data
Herbert
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic