What is the difference between the method writeChar and writeShort in java.io.DataOutputStream class?

StackOverflow https://stackoverflow.com/questions/21757707

  •  11-10-2022
  •  | 
  •  

Question

I am currently learning basic I/O of java. Please see the following code:

import java.io.DataOutputStream;
import java.io.IOException;
public class TestIO {

public static void main(String[] args) {

    try(DataOutputStream out=new 
    DataOutputStream(new FileOutputStream("e:\\ccc.txt"))){

        out.writeShort(67);
        out.writeChar(67);

    }catch(IOException ex){
        ex.printStackTrace();
    }

}

}

In the output ccc.txt file, I get the following output:

 C C

I understand that both methods write two bytes to the outputstream and the binary string of 67 is 1000011 which represents the capital letter C in ASCII or anyother code. The space before C is the byte 0000 0000 read by the methods.

However, what is the difference between the two methods if they both simply write two bytes to the outputstream?I was expecting that writeShort can write two bytes to the outputstream and then transfer it back to integer. In other words, how can I directly write the integer 67 to a file,not converting it to a character?


Let me ask this question in a different way, under what circumstance can these two methods generate different results? I need a real-world example. Thanks!

Was it helpful?

Solution 2

I suspect the confusion comes from trying to write text using classes designed to write binary. If you want to write/read text use Writer/Reader classes. If you want binary use OutputStream/InputStream.

PrintWriter pw = new PrintWriter("ccc.txt");
pw.println('C');
pw.println(67);
pw.close();

will produce a file which looks like

C
67

but in reality you have written a file which contains bytes 67, 19, 16, 54, 55, 19, 16 i.e it is now 7 bytes long.

what is the difference between the two methods if they both simply write two bytes to the outputstream?

They do exactly the same thing in this case. The difference only matters for the readChar / readShort.

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

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

You can see the code is the same.

I was expecting that writeShort can write two bytes to the outputstream and then transfer it back to integer.

A file only contains a stream of bytes.

In other words, how can I directly write the integer 67 to a file,not converting it to a character?

That is what you are doing. A file only contains bytes. When you read text you are converting these bytes into chars, or shorts or ints depending on how you read them.

OTHER TIPS

However, what is the difference between the two methods if they both simply write two bytes to the outputstream?

This sounds trite, but: one takes a char, the other takes a short. Those types aren't convertible to each other, and you use them at different times. Even though a char is just a 16-bit unsigned integer, its expected use is for UTF-16 code units; you wouldn't (or at least shouldn't) use it to store values you think of as numbers... and the opposite is true for short.

So if you've got a char that you want to write, use writeChar. If you've got a short, use writeShort. In both cases you'd want to read with the corresponding method.

writeShort(int v) 

Writes two bytes to the output stream to represent the value of the argument.

writeChar(int v) 

Writes a char value, which is comprised of two bytes, to the output stream.

You could use:

writeInt(int v) 

Writes an int value, which is comprised of four bytes, to the output stream.

In general, you do not want to use writeChar() to write text data to output streams. You'll want to use a Writer with the proper encoding.

If you need to output a binary 16-bit signed integer for some reason, you can use writeShort, but if you really need that much control you may want to use a ByteBuffer instead.

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