• 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

char = 1 byte ?? full code from jawroski quiz

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is question from jawroski which says char = 1 byte how is it possible ?

here is the full code from jawroski quiz i am adding
How many bytes does the fallowing program write to temp.txt?
import java.io.*;
public class TestIOApp{
public static void main(String args{})throws IOException{
FileOutputStream outStream = new FileOutputStream("text.txt");
String s = "text";
for(int i =0 ;i<s.length();++i)>
outStream.write(s.charat(i));
outStream.close();
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the prototype of the write method for FileOutputStream:
public native void write(int b) throws IOException
The API documentation says:
Writes the specified byte to this file output stream. Implements the write method of OutputStream.
If you are writing a char to the output stream using some other methods, the no. of bytes written will be different. However, here the method only writes one byte. It internally gets the last 8 bits (the char is promoted to int when passed as an argument) and then writes to the output stream. It does not mean that 1 char = 1 byte.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kameshwar,
FileOutputStream's write(int i) method writes only the lower 8 bits out of the 32 bit int input arg value. Since you write the String "text" ,totally 4 bytes are written to file "text.txt". Simillary while reading, FileInputStream reads ONLY ONE byte at a time and, returns the single read byte from file, as an 'int'. All higher 24 bits are put as 0 (zero bits).
I have slightly modified your code. It writes the String "text" to "text.txt" file and reads it back from the file and prints it.
regds
maha anna
<pre>

import java.io.*;
class test{
public static void main(String args[])throws IOException{
FileOutputStream outStream = new FileOutputStream("text.txt");
String s = "text";
for(int i =0 ;i < s.length();++i)<br /> outStream.write(s.charAt(i));<br /> outStream.close();<br /> <br /> FileInputStream inStream = new FileInputStream("text.txt");<br /> int i=0;<br /> while( (i=inStream.read()) > 0)
System.out.println((char)i);
inStream.close();
}
}

</pre>

[This message has been edited by maha anna (edited April 30, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic