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(); }
readUTF那边有什么问题呢
怎么传不了图片啊
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readUTF(DataInputStream.java:609)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at com.imooc.io.DisDemo.main(DisDemo.java:20)
第20行就是上面的第10行
public static void main(String args[]) throws IOException{ String file = "demo\\dos.dat"; DataOutputStream dos = new DataOutputStream( new FileOutputStream(file)); int n = 1;long m = 2; String string = "去"; dos.writeInt(n); dos.writeLong(m); dos.writeUTF(string); dos.close(); DataInputStream dis = new DataInputStream( new FileInputStream(file)); int i=dis.readInt(); System.out.println(i); long l=dis.readLong();System.out.println(l); String s=dis.readUTF();System.out.println(s); dis.close(); }
我这个没报错,你试试
也可以,你是先运行DataOutputStream,再运行DataInputStream的吧
public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; 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(); }
public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; DataOutputStream dos = new DataOutputStream(new FileOutputStream(file)); dos.writeInt(5);dos.writeInt(10);dos.writeLong(20l);dos.writeDouble(10.5); dos.writeUTF("中国"); dos.close(); IOUtil.printHex(file); }
。。。。。。。。。。。。。。。。。
那就好,采纳吧哈哈哈哈
莫非是因为你把文件用16进制打印了
我运行出来了,,,尴尬
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.writeLong(20l);dos.writeDouble(10.5); dos.writeUTF("中国"); dos.close(); IOUtil.printHex(file); }
public static void main(String[] args) throws IOException { String file = "demo/dos.dat"; DataInputStream dis = new DataInputStream(new FileInputStream(file)); int 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(); }
意思应该是读到文件结尾还在读吧,确保读之前,有字符串写进去吗