为什么我在写入以下代码的文件时会得到额外的字符?如果我使用writeBytes(String)
的是下面的代码是工作文件。那么dos.writeChars()
方法有什么问题呢?
File fileObj = new File("student.txt");
try {
// writing into file
FileOutputStream fos = new FileOutputStream(fileObj);
String msg = "This is student file";
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(msg);
//reading from file
FileInputStream fis = new FileInputStream(fileObj);
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readLine());
for (int i = 0; ((i = dis.read()) != -1); i++) {
System.out.println(i);
}
fos.close();
dos.close();
} catch (FileNotFoundException e) {
System.err.println("File not found!");
} catch (IOException e) {
e.printStackTrace();
}