import java.io.*;
class PrimitiveIO {
public static void main(String args[]) {
try {
FileOutputStream fileOutStrm = new FileOutputStream("AAA.txt");
BufferedOutputStream bufOutStrm = new BufferedOutputStream(fileOutStrm);
DataOutputStream outStream = new DataOutputStream(bufOutStrm);
outStream.writeChar('A');
outStream.writeFloat((float)3.14);
outStream.writeInt(2012);
outStream.writeBoolean(false);
outStream.flush();
FileInputStream fileInStrm = new FileInputStream("AAA.txt");
BufferedInputStream bufInStrm = new BufferedInputStream(fileInStrm);
DataInputStream inStream = new DataInputStream(bufInStrm);
System.out.println(inStream.readChar());
System.out.println(inStream.readFloat());
System.out.println(inStream.readInt());
System.out.println(inStream.readBoolean());
inStream.close();
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
}
}
代码没错,而且也能写入文件,在控制台能正常显示,但为何写入文件的都是乱码?
3 个解决方案
#1
学习了,google的
http://wenwen.soso.com/z/q194521618.htm
总之应该使用OutputStreamWriter
http://wenwen.soso.com/z/q194521618.htm
总之应该使用OutputStreamWriter
#2
不是乱码,其实是数据的二进制格式写入的...
比如说:outStream.writeChar('A');
二进制查看文件就是:0x41
#3
设置一下文件的编码
#1
学习了,google的
http://wenwen.soso.com/z/q194521618.htm
总之应该使用OutputStreamWriter
http://wenwen.soso.com/z/q194521618.htm
总之应该使用OutputStreamWriter
#2
不是乱码,其实是数据的二进制格式写入的...
比如说:outStream.writeChar('A');
二进制查看文件就是:0x41
#3
设置一下文件的编码