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.
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. 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) { }
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.
使用嵌套是为了得到更多流处理方法。
的确,FileOutputStream能够把内容写到文件中去,但它没有缓冲机制来提高效率,用BufferedOutputStream嵌套一下就能搞定了。
而你如果希望能够直接写基本类型的数据而不是单个字节的数据的话,就可以再用DataOutputStream包装一下,它提供writeBoolean、writeInt(int v) 等特有的方法。
因此,是否使用嵌套,看你的需要。
对于
3. FileInputStream fis = new FileInputStream(“fname”);
4. DataInputStream dis = new DataInputStream(fis);因为你可能要操作int 或是string 而不是bytes 所以你需要FilterOutputStream ,所以你需要流嵌套
而且没有缓冲,所以就套上了其他的输出流,
以便得到一些FileInputStream没有的write什么什么方法!是这样吧!