出错了....
我是照着老师的代码敲的,可是有异常,而且double的值输出的不对,应该是10.5的,请问那位知道哪里错了,求指教...
import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DosDemo { public static void main(String[] args)throws IOException { String file="demo/dos.dat"; DataOutputStream dos=new DataOutputStream(new FileOutputStream(file)); dos.writeInt(10); dos.writeInt(-10); dos.writeLong(10l); dos.writeDouble(10.5); dos.writeUTF("中国");//用utf-8编码显示 dos.writeChars("中国");//utf-16be dos.close(); IOUtil.printHex(file); } }
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class DisDemo { public static void main(String[] args) throws IOException{ String file="demo/dos.dat"; IOUtil.printHex(file); DataInputStream dis=new DataInputStream(new FileInputStream(file)); int i=dis.readInt(); System.out.println(i); i=dis.readInt(); System.out.println(i); long l=dis.readLong(); System.out.println(l); double d=dis.readDouble(); System.out.println(d); String s=dis.readUTF(); System.out.println(s); dis.close(); } }
结果是这样的:
00 00 00 0a ff ff ff f6 00 00
00 00 00 00 00 0a 00 06 e4 b8
ad e5 9b bd 4e 2d 56 fd 10
-10
10
9.58651235932276E-309
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at javaIO.DisDemo.main(DisDemo.java:25)