简体   繁体   English

关于DataOutputStream的困惑

[英]Confusion about DataOutputStream

I wrote the code below to familiarise myself with DataOutputStream. 我编写了以下代码,以熟悉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. 我想在文件中我会在其中输出数据的二进制文件类型为11010111 ,相反,只有一些可读的字符串。 My question is: shouldn't there be binary data in the file while DataOutputStream is used to output data? 我的问题是:使用DataOutputStream输出数据时,文件中不应该存在二进制数据吗?

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();
}

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. writeChars方法将字符串的每个char写为两个字节。 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. 如果在十六进制查看器中打开文件,则会看到每个字母的ASCII码之前有一个NUL( 00 )字节。 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. 它会跳过NUL字节并仅显示可读字节,或者将字节解释为以UTF-16LE编码的文本。

Since you use writeChars() you only output the characters. 由于使用writeChars() ,因此仅输出字符。 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. 每个字符就像通过writeChar方法一样写入数据输出流。 If no exception is thrown, the counter written is incremented by twice the length of s. 如果未引发异常,则写入的计数器将增加s长度的两倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM