DataOutputStream
DataOutputStreams是OutputStream的子类。
是数据输出流,此类继承自FillterOutputStream类,同时实现DataOutput接口。在DataOutput接口定义了一系列写入各种数据的方法。
从类 java.io.FilterOutputStream 继承的方法有:close,write
从接口 java.io.DataOutput 继承的方法有:write
字节处理流,提供了多个很常用的方法。
是对普通流的功能的一个扩展,可以更加方便地读取int,long,字符等类型数据 。
数据流: DataInputStream 和 DataOutputStream
- 1). DataInputStream 数据的字节输入流; DataOutputStream 数据的字节输出流。
- 2). 功能: 实现八种基本类型数据的输入/输出。 同时,也可实现字符串的输入/输出。
- 3). 特点: 八种基本类型的数据在输入/输出时,会保持类型不变。
- 因此,这种流特别适合在网络上实现基本类型数据和字符串的传递。
- 4). 方法:
- readByte() writeByte()
- readShort() writeShort();
- readInt() writeInt();
- readLong() writeLong();
- readFloat() writeFloat();
- readDouble() writeDouble();
- readBoolean() writeBoolean();
- readChar() writeChar();
- readUTF(); writeUTF();
- 5). 数据流属于处理流,它必须套接在节点流。
- 6). 注意: 数据流在读取与存储时的顺序要一致。否则,读取数据会失真。
对DataOutputStream源码的分析
package java.io;
public class DataOutputStream extends FilterOutputStream implements DataOutput {
//字节数,该变量用于记录实际写入数据的字节数,当写入的数据超过int型数据的表达范围时,该值将被赋予Integer.MAX_VALUE
protected int written;
//对应的字节数组
private byte[] bytearr = null;
//构造函数:一个带一个参数的构造方法,传入的参数是一个OutputStream对象,内部调用FilterOutputStream对象的构造参数。
public DataOutputStream(OutputStream out) {
super(out);
}
//增加written
private void incCount(int value) {
int temp = written + value;
if (temp < 0) {//这里的temp<0的情况其实就是int型变量数据溢出了。
temp = Integer.MAX_VALUE;
}
written = temp;
}
// 将int类型的值写入到“数据输出流”中
public synchronized void write(int b) throws IOException {
out.write(b);
incCount(1);
}
// 将字节数组b从off开始的len个字节,都写入到“数据输出流”中
public synchronized void write(byte b[], int off, int len)
throws IOException
{
out.write(b, off, len);
incCount(len);
}
// 清空缓冲,即将缓冲中的数据都写入到输出流中
public void flush() throws IOException {
out.flush();
}
public final void writeBoolean(boolean v) throws IOException {
out.write(v ? 1 : 0);
incCount(1);
}
public final void writeByte(int v) throws IOException {
out.write(v);
incCount(1);
}
// 注意:short占2个字节
public final void writeShort(int v) throws IOException {
out.write((v >>> 8) & 0xFF);// 写入 short高8位 对应的字节
out.write((v >>> 0) & 0xFF); // 写入 short低8位 对应的字节
incCount(2);
}
// 注意:char占2个字节
public final void writeChar(int v) throws IOException {
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(2);
}
// 注意:int占4个字节
public final void writeInt(int v) throws IOException {
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(4);
}
private byte writeBuffer[] = new byte[8];
// 注意:long占8个字节
public final void writeLong(long v) throws IOException {
writeBuffer[0] = (byte)(v >>> 56);
writeBuffer[1] = (byte)(v >>> 48);
writeBuffer[2] = (byte)(v >>> 40);
writeBuffer[3] = (byte)(v >>> 32);
writeBuffer[4] = (byte)(v >>> 24);
writeBuffer[5] = (byte)(v >>> 16);
writeBuffer[6] = (byte)(v >>> 8);
writeBuffer[7] = (byte)(v >>> 0);
out.write(writeBuffer, 0, 8);
incCount(8);
}
// 将float类型的值写入到“数据输出流”中
public final void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
}
// 将double类型的值写入到“数据输出流”中
public final void writeDouble(double v) throws IOException {
writeLong(Double.doubleToLongBits(v));
}
// 将String类型的值写入到“数据输出流”中,实际写入时,是将String对应的每个字符转换成byte数据后写入输出流中。
public final void writeBytes(String s) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
out.write((byte)s.charAt(i));
}
incCount(len);
}
// 将String类型的值写入到“数据输出流”中, 实际写入时,是将String对应的每个字符转换成char数据后写入输出流中。
public final void writeChars(String s) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
int v = s.charAt(i);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
}
incCount(len * 2);
}
// 将UTF-8类型的值写入到“数据输出流”中
public final void writeUTF(String str) throws IOException {
writeUTF(str, this);
}
// 将String数据以UTF-8类型的形式写入到“输出流out”中
static int writeUTF(String str, DataOutput out) throws IOException {
int strlen = str.length();//获取String的长度
int utflen = 0;
int c, count = 0;
// 由于UTF-8是1~4个字节不等;这里,根据UTF-8首字节的范围,判断UTF-8是几个字节的。
/* use charAt instead of copying String to char array */
for (int i = 0; i < strlen; i++) {
c = str.charAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
utflen++;//一个字节
} else if (c > 0x07FF) {
utflen += 3;//三个字节
} else {
utflen += 2;//两个字节
}
}
if (utflen > 65535)//(65535)10=(1111111111111111)2
throw new UTFDataFormatException(
"encoded string too long: " + utflen + " bytes");
byte[] bytearr = null;// 新建“字节数组bytearr”
//定义了一个字节数组,用于充当向流中写入数据时的临时缓存。
//先判断传入的out对象是否是DataOuputStream类或者其子类,如果是,就使用其内部定义的bytearr数组,否则就新建一个byte数组赋值给bytearr。
//如果使用内置的数组,会先对内置数组进行检测,检测其是否为null和容量是否满足需求,如果不满足也要重新创建。
if (out instanceof DataOutputStream) {
DataOutputStream dos = (DataOutputStream)out;
if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))
//这里就是内置数组为null或者容量不够时,重新创建数组对象,新建的容量会根据utflen来进行扩容,这里注意到所有的数组容量中都有+2的操作,这是因为除了要写入数据外,该方法还会在开头处写入两个字节的数据,这两个字节的数据记录了流中数据的总长度。
dos.bytearr = new byte[(utflen*2) + 2];
bytearr = dos.bytearr;
} else {
bytearr = new byte[utflen+2];
}
// “字节数组”的前2个字节保存的是“UTF-8数据的长度”
bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
// 对UTF-8中的单字节数据进行预处理,如果是utf8中的单字节数据,则直接装换成byte型写入数据缓存中。
int i=0;
for (i=0; i<strlen; i++) {
c = str.charAt(i);
if (!((c >= 0x0001) && (c <= 0x007F))) break;
bytearr[count++] = (byte) c;
}
// 对预处理后的数据,接着进行处理
for (;i < strlen; i++){
c = str.charAt(i);
// UTF-8数据是1个字节的情况
if ((c >= 0x0001) && (c <= 0x007F)) {
bytearr[count++] = (byte) c;
} else if (c > 0x07FF) { // UTF-8数据是3个字节的情况
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
} else { // UTF-8数据是2个字节的情况
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}// 将字节数组写入到“数据输出流”中
out.write(bytearr, 0, utflen+2);
//最终返回以utf8格式写入流中的字节数
return utflen + 2;
}
//返回当前写入流中数据的字节总数
public final int size() {
return written;
}
}
为了方便理解,这里附上一幅utf8编码的一些小格式:
最后附上一个小例子:
import java.io.*;
/**
* @Author: lishi
* @Description:
* @Date: Create in 18:40 2019/3/25
*/
public class DataOutputStream0325 {
public static void main(String args[]) throws Exception{ // 所有异常抛出
DataOutputStream dos = null ; // 声明数据输出流对象
File f = new File("C:\\Users\\lishi\\Desktop\\test.txt") ; // 文件的保存路径
dos = new DataOutputStream(new FileOutputStream(f)) ; // 实例化数据输出流对象
String names[] = {"衬衣","手套","围巾"} ; // 商品名称
float prices[] = {98.3f,30.3f,50.5f} ; // 商品价格
int nums[] = {3,2,1} ; // 商品数量
for(int i=0;i<names.length;i++){ // 循环输出
dos.writeChars(names[i]) ; // 写入字符串,注意,这边少数writeChars(),不是writechar()。
dos.writeChar('\t') ; // 写入分隔符,这边是读取writechar()。
dos.writeFloat(prices[i]) ; // 写入价格
dos.writeChar('\t') ; // 写入分隔符
dos.writeInt(nums[i]) ; // 写入数量
dos.writeChar('\n') ; // 换行
}
dos.close() ; // 关闭输出流
DataInputStream dis = null ; // 声明数据输入流对象
// File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路径
dis = new DataInputStream(new FileInputStream(f)) ; // 实例化数据输入流对象
String name = null ; // 接收名称
float price = 0.0f ; // 接收价格
int num = 0 ; // 接收数量
char temp[] = null ; // 接收商品名称
int len = 0 ; // 保存读取数据的个数
char c = 0 ; // '\u0000'
try{
while(true){
temp = new char[200] ; // 开辟空间
len = 0 ;
while((c=dis.readChar())!='\t'){ // 接收内容,因为直到读取到'\t'才完成了读取一个字符串,未读取到表示还有内容。
temp[len] = c ;
len ++ ; // 读取长度加1
}
name = new String(temp,0,len) ; // 将字符数组变为String
price = dis.readFloat() ; // 读取价格
dis.readChar() ; // 读取\t
num = dis.readInt() ; // 读取int
dis.readChar() ; // 读取\n
System.out.printf("名称:%s;价格:%5.2f;数量:%d\n",name,price,num) ;
}
}catch(Exception e){}
dis.close() ;
}
}