Question

The problem is when I write a character using FileOutputStream it is readable. But when I chain it with DataOutputStream the written data becomes unreadable.

Why is that ? Since both FileOutputStream and DataInputStream write in bytes to the file. How exactly will the processing happen ?

Code:

File newFile = new File("C:\\Jeevantest.as");
FileOutputStream outFp = new FileOutputStream(newFile);
outFp.write('X');
outFp.close();

In the file Jeevantest.as, the char 'X' can be seen. 

File newFile = new File("C:\\Jeevantest.as");
FileOutputStream outFp = new FileOutputStream(newFile);
DataOutputStream dp = new DataOutputStream(outFp);
dp.writeChar('J');
outFp.close();

In this case, the following output is shown:

Output

Need to understand why ?

Was it helpful?

Solution

The difference resides not in the OutputStreams but in the write methods you are using: they differ. In your first example you use dp.write() and in your second example you use dp.writeChar().

Change dp.writeChar() to dp.write() in your second example and the results will be as expected.

java.io.DataOutputStream.writeChar(int) method is implemented as follows:

public final void writeChar(int v) throws IOException {
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
    incCount(2);
}

See soource code of writeChar(int)

while java.io.DataOutputStream.write(int) has following implementation:

 public synchronized void write(int b) throws IOException {
     out.write(b);
     incCount(1);
 }

See soource code of write(int)

OTHER TIPS

writeChar writes char as a 2-byte value in UTF-16 encoding. In case 'J' it is 00 4A. To display it correctly you need to use correct encoding

DataOutputStream is used to store primitive in a portable way.
Look how a char is written to stream

public final void writeChar(int v) throws IOException {
  out.write((v >>> 8) & 0xFF);
  out.write((v >>> 0) & 0xFF);
  incCount(2);
}

A char is write using 2 bytes, high byte first. Because hi-byte is 0 (in your case 'J' => 004A) the result is what you see in you file.

File newFile = new File("C:\\Jeevantest.as");
FileOutputStream outFp = new FileOutputStream(newFile);
DataOutputStream dp = new DataOutputStream(outFp);
dp.writeChars("J");
outFp.close();
dp.close();
  • First of all writeChar takes int argument so, if you need to pass a string you need to use writeChars(String arg)
  • Close dp stream.

The stream consists of binary data with nothing to indicate the type of individual values, or where they begin in the stream. So it can only be read using a DataInputStream. Check this oracle official tutorial for further information: http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

The stream consists of binary data with nothing to indicate the type of individual values, or where they begin in the stream. So it can only be read using a DataInputStream. Check this link for further information http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top