特殊流(DataInputStream与DataOutputStream)分析
特殊流/数据流(DataInputStream与DataOutputStream)引用的是字节流,所以DataInputStream与DataOutputStream属于高级流。
DataInputStream
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本Java数据类型。应用程序可以使用数据输出流写入稍后有数据输入流读取的数据。DateInputStream对于多线程访问不一定是安全的。线程安全是可选的,它由此类方法的使用者负责。
**
源码分析
**
继承关系
class DataInputStream extends FilterInputStream implements
第一种读取方式,无参
/**
/* int read() throws IOException {
return in.read()
/*读取第一个字节,返回值为int类型(使用FilterInputStream中的read()方法)
*/
System.out.println(dataInputStream.read());
第二种读取方式,参数为byte[]
/**
* final int read(byte b[]) throws IOException {
* return in.read(b, 0, b.length);
* }
* 返回值为int类型,返回结果为读取到的实际个数
*/
byte [] b=new byte[30];
System.out.println(dataInputStream.read(b));
System.out.println(Arrays.toString(b));
第三种读取方式,有参
/**
*final int read(byte b[], int off, int len) throws IOException {
* return in.read(b, off, len);
* 返回值:int类型,实际读取到的个数
* 参数:
* byte b[]:表示读取到的数据存储到该数组
* int off:表示从存储数组的偏移位置off开始存储字符
* int len:读取的数据的长度
*/
byte [] b=new byte[30];
System.out.println(dataInputStream.read(b,0,5));
System.out.println(Arrays.toString(b));
特有方法
readUTF读取由writeUTF修改的数据。
/**
* 特殊方法
* final String readUTF() throws IOException {
* return readUTF(this);
*/