java io系列15之 DataOutputStream(数据输出流)的认知、源码和示例

简介: 本章介绍DataOutputStream。我们先对DataOutputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解。 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_15.html DataOutputStream 介绍 DataOutputStream 是数据输出流。

本章介绍DataOutputStream。我们先对DataOutputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解。

转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_15.html

DataOutputStream 介绍

DataOutputStream 是数据输出流。它继承于FilterOutputStream。
DataOutputStream 是用来装饰其它输出流,将DataOutputStream和DataInputStream输入流配合使用,“允许应用程序以与机器无关方式从底层输入流中读写基本 Java 数据类型”。

DataOutputStream 源码分析(基于jdk1.7.40)

复制代码
  1 package java.io;
  2 
  3 public class DataOutputStream extends FilterOutputStream implements DataOutput {  4 // “数据输出流”的字节数  5 protected int written;  6  7 // “数据输出流”对应的字节数组  8 private byte[] bytearr = null;  9  10 // 构造函数  11 public DataOutputStream(OutputStream out) {  12 super(out);  13  }  14  15 // 增加“输出值”  16 private void incCount(int value) {  17 int temp = written + value;  18 if (temp < 0) {  19 temp = Integer.MAX_VALUE;  20  }  21 written = temp;  22  }  23  24 // 将int类型的值写入到“数据输出流”中  25 public synchronized void write(int b) throws IOException {  26  out.write(b);  27 incCount(1);  28  }  29  30 // 将字节数组b从off开始的len个字节,都写入到“数据输出流”中  31 public synchronized void write(byte b[], int off, int len)  32 throws IOException  33  {  34  out.write(b, off, len);  35  incCount(len);  36  }  37  38 // 清空缓冲,即将缓冲中的数据都写入到输出流中  39 public void flush() throws IOException {  40  out.flush();  41  }  42  43 // 将boolean类型的值写入到“数据输出流”中  44 public final void writeBoolean(boolean v) throws IOException {  45 out.write(v ? 1 : 0);  46 incCount(1);  47  }  48  49 // 将byte类型的值写入到“数据输出流”中  50 public final void writeByte(int v) throws IOException {  51  out.write(v);  52 incCount(1);  53  }  54  55 // 将short类型的值写入到“数据输出流”中  56 // 注意:short占2个字节  57 public final void writeShort(int v) throws IOException {  58 // 写入 short高8位 对应的字节  59 out.write((v >>> 8) & 0xFF);  60 // 写入 short低8位 对应的字节  61 out.write((v >>> 0) & 0xFF);  62 incCount(2);  63  }  64  65 // 将char类型的值写入到“数据输出流”中  66 // 注意:char占2个字节  67 public final void writeChar(int v) throws IOException {  68 // 写入 char高8位 对应的字节  69 out.write((v >>> 8) & 0xFF);  70 // 写入 char低8位 对应的字节 71 out.write((v >>> 0) & 0xFF); 72 incCount(2); 73 } 74 75 // 将int类型的值写入到“数据输出流”中 76 // 注意:int占4个字节 77 public final void writeInt(int v) throws IOException { 78 out.write((v >>> 24) & 0xFF); 79 out.write((v >>> 16) & 0xFF); 80 out.write((v >>> 8) & 0xFF); 81 out.write((v >>> 0) & 0xFF); 82 incCount(4); 83 } 84 85 private byte writeBuffer[] = new byte[8]; 86 87 // 将long类型的值写入到“数据输出流”中 88 // 注意:long占8个字节 89 public final void writeLong(long v) throws IOException { 90 writeBuffer[0] = (byte)(v >>> 56); 91 writeBuffer[1] = (byte)(v >>> 48); 92 writeBuffer[2] = (byte)(v >>> 40); 93 writeBuffer[3] = (byte)(v >>> 32); 94 writeBuffer[4] = (byte)(v >>> 24); 95 writeBuffer[5] = (byte)(v >>> 16); 96 writeBuffer[6] = (byte)(v >>> 8); 97 writeBuffer[7] = (byte)(v >>> 0); 98 out.write(writeBuffer, 0, 8); 99 incCount(8); 100 } 101 102 // 将float类型的值写入到“数据输出流”中 103 public final void writeFloat(float v) throws IOException { 104 writeInt(Float.floatToIntBits(v)); 105 } 106 107 // 将double类型的值写入到“数据输出流”中 108 public final void writeDouble(double v) throws IOException { 109 writeLong(Double.doubleToLongBits(v)); 110 } 111 112 // 将String类型的值写入到“数据输出流”中 113 // 实际写入时,是将String对应的每个字符转换成byte数据后写入输出流中。 114 public final void writeBytes(String s) throws IOException { 115 int len = s.length(); 116 for (int i = 0 ; i < len ; i++) { 117 out.write((byte)s.charAt(i)); 118 } 119 incCount(len); 120 } 121 122 // 将String类型的值写入到“数据输出流”中 123 // 实际写入时,是将String对应的每个字符转换成char数据后写入输出流中。 124 public final void writeChars(String s) throws IOException { 125 int len = s.length(); 126 for (int i = 0 ; i < len ; i++) { 127 int v = s.charAt(i); 128 out.write((v >>> 8) & 0xFF); 129 out.write((v >>> 0) & 0xFF); 130 } 131 incCount(len * 2); 132 } 133 134 // 将UTF-8类型的值写入到“数据输出流”中 135 public final void writeUTF(String str) throws IOException { 136 writeUTF(str, this); 137 } 138 139 // 将String数据以UTF-8类型的形式写入到“输出流out”中 140 static int writeUTF(String str, DataOutput out) throws IOException { 141 //获取String的长度 142 int strlen = str.length(); 143 int utflen = 0; 144 int c, count = 0; 145 146 // 由于UTF-8是1~4个字节不等; 147 // 这里,根据UTF-8首字节的范围,判断UTF-8是几个字节的。 148 for (int i = 0; i < strlen; i++) { 149 c = str.charAt(i); 150 if ((c >= 0x0001) && (c <= 0x007F)) { 151 utflen++; 152 } else if (c > 0x07FF) { 153 utflen += 3; 154 } else { 155 utflen += 2; 156 } 157 } 158 159 if (utflen > 65535) 160 throw new UTFDataFormatException( 161 "encoded string too long: " + utflen + " bytes"); 162 163 // 新建“字节数组bytearr” 164 byte[] bytearr = null; 165 if (out instanceof DataOutputStream) { 166 DataOutputStream dos = (DataOutputStream)out; 167 if(dos.bytearr == null || (dos.bytearr.length < (utflen+2))) 168 dos.bytearr = new byte[(utflen*2) + 2]; 169 bytearr = dos.bytearr; 170 } else { 171 bytearr = new byte[utflen+2]; 172 } 173 174 // “字节数组”的前2个字节保存的是“UTF-8数据的长度” 175 bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); 176 bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); 177 178 // 对UTF-8中的单字节数据进行预处理 179 int i=0; 180 for (i=0; i<strlen; i++) { 181 c = str.charAt(i); 182 if (!((c >= 0x0001) && (c <= 0x007F))) break; 183 bytearr[count++] = (byte) c; 184 } 185 186 // 对预处理后的数据,接着进行处理 187 for (;i < strlen; i++){ 188 c = str.charAt(i); 189 // UTF-8数据是1个字节的情况 190 if ((c >= 0x0001) && (c <= 0x007F)) { 191 bytearr[count++] = (byte) c; 192 193 } else if (c > 0x07FF) { 194 // UTF-8数据是3个字节的情况 195 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 196 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 197 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 198 } else { 199 // UTF-8数据是2个字节的情况 200 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 201 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 202 } 203 } 204 // 将字节数组写入到“数据输出流”中 205 out.write(bytearr, 0, utflen+2); 206 return utflen + 2; 207 } 208 209 public final int size() { 210 return written; 211 } 212 }
复制代码

示例代码

关于DataOutStream中API的详细用法,参考示例代码(DataInputStreamTest.java)

复制代码
  1 import java.io.DataInputStream;
  2 import java.io.DataOutputStream;  3 import java.io.ByteArrayInputStream;  4 import java.io.File;  5 import java.io.InputStream;  6 import java.io.FileInputStream;  7 import java.io.FileOutputStream;  8 import java.io.IOException;  9 import java.io.FileNotFoundException;  10 import java.lang.SecurityException;  11  12 /**  13  * DataInputStream 和 DataOutputStream测试程序  14  *  15  * @author skywang  16 */  17 public class DataInputStreamTest {  18  19 private static final int LEN = 5;  20  21 public static void main(String[] args) {  22 // 测试DataOutputStream,将数据写入到输出流中。  23  testDataOutputStream() ;  24 // 测试DataInputStream,从上面的输出流结果中读取数据。  25  testDataInputStream() ;  26  }  27  28 /**  29  * DataOutputStream的API测试函数  30 */  31 private static void testDataOutputStream() {  32  33 try {  34 File file = new File("file.txt");  35 DataOutputStream out =  36 new DataOutputStream(  37 new FileOutputStream(file));  38  39 out.writeBoolean(true);  40 out.writeByte((byte)0x41);  41 out.writeChar((char)0x4243);  42 out.writeShort((short)0x4445);  43 out.writeInt(0x12345678);  44 out.writeLong(0x0FEDCBA987654321L);  45  46 out.writeUTF("abcdefghijklmnopqrstuvwxyz严12");  47  48  out.close();  49 } catch (FileNotFoundException e) {  50  e.printStackTrace();  51 } catch (SecurityException e) {  52  e.printStackTrace();  53 } catch (IOException e) {  54  e.printStackTrace();  55  }  56  }  57 /**  58  * DataInputStream的API测试函数  59 */  60 private static void testDataInputStream() {  61  62 try {  63 File file = new File("file.txt");  64 DataInputStream in =  65 new DataInputStream(  66 new FileInputStream(file));  67  68 System.out.printf("byteToHexString(0x8F):0x%s\n", byteToHexString((byte)0x8F));  69 System.out.printf("charToHexString(0x8FCF):0x%s\n", charToHexString((char)0x8FCF));  70  71 System.out.printf("readBoolean():%s\n", in.readBoolean());  72 System.out.printf("readByte():0x%s\n", byteToHexString(in.readByte()));  73 System.out.printf("readChar():0x%s\n", charToHexString(in.readChar()));  74 System.out.printf("readShort():0x%s\n", shortToHexString(in.readShort()));  75 System.out.printf("readInt():0x%s\n", Integer.toHexString(in.readInt()));  76 System.out.printf("readLong():0x%s\n", Long.toHexString(in.readLong()));  77 System.out.printf("readUTF():%s\n", in.readUTF());  78  79  in.close();  80 } catch (FileNotFoundException e) {  81  e.printStackTrace();  82 } catch (SecurityException e) { 83 e.printStackTrace(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 } 88 89 // 打印byte对应的16进制的字符串 90 private static String byteToHexString(byte val) { 91 return Integer.toHexString(val & 0xff); 92 } 93 94 // 打印char对应的16进制的字符串 95 private static String charToHexString(char val) { 96 return Integer.toHexString(val); 97 } 98 99 // 打印short对应的16进制的字符串 100 private static String shortToHexString(short val) { 101 return Integer.toHexString(val & 0xffff); 102 } 103 }
复制代码

运行结果

byteToHexString(0x8F):0x8f
charToHexString(0x8FCF):0x8fcf
readBoolean():true
readByte():0x41
readChar():0x4243
readShort():0x4445
readInt():0x12345678
readLong():0xfedcba987654321
readUTF():abcdefghijklmnopqrstuvwxyz严12

结果说明

参考"java io系列14之 DataInputStream(数据输入流)的认知、源码和示例"

相关文章
|
11小时前
|
监控 Java
Java一分钟之-NIO:非阻塞IO操作
【5月更文挑战第14天】Java的NIO(New IO)解决了传统BIO在高并发下的低效问题,通过非阻塞方式提高性能。NIO涉及复杂的选择器和缓冲区管理,易出现线程、内存和中断处理的误区。要避免这些问题,可以使用如Netty的NIO库,谨慎设计并发策略,并建立标准异常处理。示例展示了简单NIO服务器,接收连接并发送欢迎消息。理解NIO工作原理和最佳实践,有助于构建高效网络应用。
8 2
|
11小时前
|
Java 数据挖掘 BI
Java医院绩效考核系统源码B/S+avue+MySQL助力医院实现精细化管理
医院绩效考核系统目标是实现对科室、病区财务指标、客户指标、流程指标、成长指标的全面考核、分析,并与奖金分配、学科建设水平评价挂钩。
30 0
|
11小时前
|
数据采集 前端开发 Java
Java医院绩效考核系统源码maven+Visual Studio Code一体化人力资源saas平台系统源码
医院绩效解决方案包括医院绩效管理(BSC)、综合奖金核算(RBRVS),涵盖从绩效方案的咨询与定制、数据采集、绩效考核及反馈、绩效奖金核算到科到组、分配到员工个人全流程绩效管理;将医院、科室、医护人员利益绑定;全面激活人才活力;兼顾质量和效益、长期与短期利益;助力医院降本增效,持续改善、优化收入、成本结构。
15 0
|
11小时前
|
SQL Java
java处理数据查看范围
java处理数据查看范围
|
11小时前
|
Java 开发者
Java一分钟之-Java IO流:文件读写基础
【5月更文挑战第10天】本文介绍了Java IO流在文件读写中的应用,包括`FileInputStream`和`FileOutputStream`用于字节流操作,`BufferedReader`和`PrintWriter`用于字符流。通过代码示例展示了如何读取和写入文件,强调了常见问题如未关闭流、文件路径、编码、权限和异常处理,并提供了追加写入与读取的示例。理解这些基础知识和注意事项能帮助开发者编写更可靠的程序。
17 0
|
11小时前
|
监控 前端开发 Java
Java基于B/S医院绩效考核管理平台系统源码 医院智慧绩效管理系统源码
医院绩效考核系统是一个关键的管理工具,旨在评估和优化医院内部各部门、科室和员工的绩效。一个有效的绩效考核系统不仅能帮助医院实现其战略目标,还能提升医疗服务质量,增强患者满意度,并促进员工的专业成长
18 0
|
11小时前
|
Java 云计算
Java智能区域医院云HIS系统SaaS源码
云HIS提供标准化、信息化、可共享的医疗信息管理系统,实现医患事务管理和临床诊疗管理等标准医疗管理信息系统的功能。优化就医、管理流程,提升患者满意度、基层首诊率,通过信息共享、辅助诊疗等手段,提高基层医生的服务能力构建和谐的基层医患关系。
33 2
|
11小时前
|
Java
从源码出发:JAVA中对象的比较
从源码出发:JAVA中对象的比较
19 3
|
11小时前
|
前端开发 Java 关系型数据库
Java医院绩效考核系统源码B/S架构+springboot三级公立医院绩效考核系统源码 医院综合绩效核算系统源码
作为医院用综合绩效核算系统,系统需要和his系统进行对接,按照设定周期,从his系统获取医院科室和医生、护士、其他人员工作量,对没有录入信息化系统的工作量,绩效考核系统设有手工录入功能(可以批量导入),对获取的数据系统按照设定的公式进行汇算,且设置审核机制,可以退回修正,系统功能强大,完全模拟医院实际绩效核算过程,且每步核算都可以进行调整和参数设置,能适应医院多种绩效核算方式。
30 2
|
11小时前
|
数据采集 机器学习/深度学习 Java
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像
本文介绍了如何使用Java和Apache HttpComponents库从Facebook获取图像数据。通过设置爬虫代理IP以避免限制,利用HttpClient发送请求,解析HTML找到图像链接,然后下载并保存图片。提供的Java代码示例展示了实现过程,包括创建代理配置、线程池,以及下载图片的逻辑。注意,实际应用需根据Facebook页面结构进行调整。
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像