我编写了下面的代码来熟悉 DataOutputStream。我想在我输出数据的文件中会有某种11010111
二进制文件,相反,只有一些可读的字符串。我的问题是:当DataOutputStream用于输出数据时,文件中不应该有二进制数据吗?
代码:
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();
}