DataInputStream(InputStream instream)
The constructor requires you to pass in an input stream. This instance
might be a file input stream (because FileInputStream extends Input-
Stream), an input stream from a socket, or any other kind of input stream.
When the instance of DataInputStream is called on to deliver data, it will
make some number of read() calls on instream, process the bytes, and
return an appropriate value.

解决方案 »

  1.   

    The code fragment below illustrates a small input chain:
    1. try {
    2. // Construct the chain
    3. FileInputStream fis = new FileInputStream(“fname”);
    4. DataInputStream dis = new DataInputStream(fis);
    5.
    6. // Read
    7. double d = dis.readDouble();
    8. int i = dis.readInt();
    9. String s = dis.readUTF();
    10.
    11. // Close the chain
    12. dis.close(); // Close dis first, because it
    13. fis.close(); // was created last
    14. }
    15. catch (IOException e) { }
      

  2.   

    The code expects that the first eight bytes in the file represent a double,
    the next four bytes represent an int, and the next who-knows-how-many
    bytes represent a UTF string. This means that the code that originally created the file must have been writing a double, an int, and a UTF string.
      

  3.   

    呵呵,这些能够嵌套别的字节流都是属于FilterOutputStream的子类。
    使用嵌套是为了得到更多流处理方法。
    的确,FileOutputStream能够把内容写到文件中去,但它没有缓冲机制来提高效率,用BufferedOutputStream嵌套一下就能搞定了。
    而你如果希望能够直接写基本类型的数据而不是单个字节的数据的话,就可以再用DataOutputStream包装一下,它提供writeBoolean、writeInt(int v) 等特有的方法。
    因此,是否使用嵌套,看你的需要。
      

  4.   

    上面的例子没有用道BufferedOutputStream 因为它的作用只是加一个缓存而已。
    对于
    3. FileInputStream fis = new FileInputStream(“fname”);
    4. DataInputStream dis = new DataInputStream(fis);因为你可能要操作int 或是string 而不是bytes 所以你需要FilterOutputStream ,所以你需要流嵌套
      

  5.   

    总结一下的话是不是就是:光有FileInputStream的writeXX不够用,
    而且没有缓冲,所以就套上了其他的输出流,
    以便得到一些FileInputStream没有的write什么什么方法!是这样吧!
      

  6.   

    嵌套那么多类就是为了增加流处理的功能嘛,FileInputStream的功能不够才会这样做的