Question

I wrote the code below to familiarise myself with DataOutputStream. I suppose there would be some kind of 11010111 binary in the file where I output the data, instead, there are just some readable strings. My question is: shouldn't there be binary data in the file while DataOutputStream is used to output data?

Code:

FileOutputStream fos = null;
try {
    fos = new FileOutputStream("myData.dat");
} catch (FileNotFoundException e) {
    System.out.println("File Not Found!!");
    System.exit(-1);
}
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(fos));
try {
    dos.writeChars("HELLO WORLD!!!\n");
    dos.writeChars("HELLO PRETTIES!!!\n");
    dos.writeChars("HELLO KITTY!!!");
    dos.flush();
    dos.close();
    fos.close();
} catch (IOException e1) {
    e1.printStackTrace();
}
FileInputStream fis = null;
try {
    fis = new FileInputStream("myData.dat");
} catch (FileNotFoundException e) {
    System.out.println("File Not Found!!");
    System.exit(-1);
}
BufferedReader br = new BufferedReader(new InputStreamReader(
        new DataInputStream(fis)));
String s;
try {
    while ((s = br.readLine()) != null) {
        System.out.println(s);
    }
    br.close();
    fis.close();
} catch (IOException e) {
    e.printStackTrace();
}
Was it helpful?

Solution

All data in a computer is binary. Some binary data may be readable text, and this is what happens in your case.

The writeChars method writes each char of the String as two bytes. If you opened the file in a hex viewer you'd see that there's a NUL (00) byte before the ASCII code of each letter. When you open the file in a text editor, it interpretes the bytes in the file as text, because that's what text editors do: read a bunch of (binary) data and show it to you as text.

How the editor does this depends on the editor. Either it skips the NUL bytes and shows only the readable bytes, or interpretes the bytes as text encoded in UTF-16LE.

OTHER TIPS

Since you use writeChars() you only output the characters. Be careful, because only the newline character splits the data. Inserting another newline in the middle of a string will likely cause problems. Here is some of the docs that may be of interest:

Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by the writeChar method. If no exception is thrown, the counter written is incremented by twice the length of s.

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