Java IO 其他流 -- 字节数组流,字符数组流和数据流
来源:互联网 发布:whip it 编辑:程序博客网 时间:2024/06/05 05:40
- 字节数组流
1.1 字节数组输入流ByteArrayInputStream
参考其源码方法,相对于InputStream,没有新增方法,所以使用也与InputStream一样,可以使用多态,例子如下:
public static void read(byte[] msg) throws IOException { InputStream is = new ByteArrayInputStream(msg); int len = 0; byte[] buffer = new byte[1024]; while (-1 != (len = is.read(buffer))) { System.out.println(new String(buffer, 0, len)); } is.close();}
注意:
ByteArrayInputStream中close源码如下,说明字节数组输入流无需关闭,都是存在于内存中,系统会自动释放。
/**
* Closing a ByteArrayInputStream has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an IOException
*/
public void close() throws IOException {
}
1.2 字节数组输出流ByteArrayOutputStream
参考其源码方法,相对于OutputStream,有新增方法toByteArray(),所以使用新增方法不能使用多态,例子如下:
public static byte[] write() throws IOException { String msg = "节点流--字节数组输出流"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(msg.getBytes()); return baos.toByteArray();}
类似于ByteArrayInputStream的close方法,ByteArrayOutputStream也无需手动关闭。
2 字符数组流
字符数组流–CharArrayReader CharArrayWriter
2.1字符输入流 CharArrayReader
可以操作字符数组的输入流,相对于InputStream,也没有新增方法,可以使用多态,例子可参考字节输入流,将ByteArrayInputStream改成CharArrayInputStream即可。
2.2 字符输入流 CharArrayWriter
可以操作字符数组的输出流,相对于OutputStream,有新增方法toCharArray(),使用新增方法时不能使用多态,例子可参考字节输入流,将ByteArrayOutputStream改成CharArrayOutputStream即可。
3 数据流
数据流的读入和写出数据是可以包含数据本身和数据类型。
3.1 数据输入流 DataInputStream
使用DataInputStream读取数据时必须知道写入的顺序,否则会读取失败。
3.2 数据输出流 DataOutputStream
可以写出具体的数据以及数据类型。
3.3 源码比对 如下:
package java.io;/** * A data input stream lets an application read primitive Java data * types from an underlying input stream in a machine-independent * way. An application uses a data output stream to write data that * can later be read by a data input stream. * <p> * DataInputStream is not necessarily safe for multithreaded access. * Thread safety is optional and is the responsibility of users of * methods in this class. * * @author Arthur van Hoff * @see java.io.DataOutputStream * @since JDK1.0 */public class DataInputStream extends FilterInputStream implements DataInput { public final int read(byte b[]) throws IOException { return in.read(b, 0, b.length); } public final int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); } public final boolean readBoolean() throws IOException { int ch = in.read(); if (ch < 0) throw new EOFException(); return (ch != 0); } public final byte readByte() throws IOException { int ch = in.read(); if (ch < 0) throw new EOFException(); return (byte)(ch); } public final int readUnsignedByte() throws IOException { int ch = in.read(); if (ch < 0) throw new EOFException(); return ch; } public final short readShort() throws IOException { int ch1 = in.read(); int ch2 = in.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (short)((ch1 << 8) + (ch2 << 0)); } public final int readUnsignedShort() throws IOException { int ch1 = in.read(); int ch2 = in.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (ch1 << 8) + (ch2 << 0); } public final char readChar() throws IOException { int ch1 = in.read(); int ch2 = in.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (char)((ch1 << 8) + (ch2 << 0)); } public final int readInt() throws IOException { int ch1 = in.read(); int ch2 = in.read(); int ch3 = in.read(); int ch4 = in.read(); if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException(); return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); } public final long readLong() throws IOException { readFully(readBuffer, 0, 8); return (((long)readBuffer[0] << 56) + ((long)(readBuffer[1] & 255) << 48) + ((long)(readBuffer[2] & 255) << 40) + ((long)(readBuffer[3] & 255) << 32) + ((long)(readBuffer[4] & 255) << 24) + ((readBuffer[5] & 255) << 16) + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0)); } public final float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); } public final double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); } @Deprecated public final String readLine() throws IOException { char buf[] = lineBuffer; if (buf == null) { buf = lineBuffer = new char[128]; } int room = buf.length; int offset = 0; int c;loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) { if (!(in instanceof PushbackInputStream)) { this.in = new PushbackInputStream(in); } ((PushbackInputStream)in).unread(c2); } break loop; default: if (--room < 0) { buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); lineBuffer = buf; } buf[offset++] = (char) c; break; } } if ((c == -1) && (offset == 0)) { return null; } return String.copyValueOf(buf, 0, offset); } public final String readUTF() throws IOException { return readUTF(this); } public final static String readUTF(DataInput in) throws IOException { int utflen = in.readUnsignedShort(); byte[] bytearr = null; char[] chararr = null; if (in instanceof DataInputStream) { DataInputStream dis = (DataInputStream)in; if (dis.bytearr.length < utflen){ dis.bytearr = new byte[utflen*2]; dis.chararr = new char[utflen*2]; } chararr = dis.chararr; bytearr = dis.bytearr; } else { bytearr = new byte[utflen]; chararr = new char[utflen]; } int c, char2, char3; int count = 0; int chararr_count=0; in.readFully(bytearr, 0, utflen); while (count < utflen) { c = (int) bytearr[count] & 0xff; if (c > 127) break; count++; chararr[chararr_count++]=(char)c; } while (count < utflen) { c = (int) bytearr[count] & 0xff; switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: /* 0xxxxxxx*/ count++; chararr[chararr_count++]=(char)c; break; case 12: case 13: /* 110x xxxx 10xx xxxx*/ count += 2; if (count > utflen) throw new UTFDataFormatException( "malformed input: partial character at end"); char2 = (int) bytearr[count-1]; if ((char2 & 0xC0) != 0x80) throw new UTFDataFormatException( "malformed input around byte " + count); chararr[chararr_count++]=(char)(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: /* 1110 xxxx 10xx xxxx 10xx xxxx */ count += 3; if (count > utflen) throw new UTFDataFormatException( "malformed input: partial character at end"); char2 = (int) bytearr[count-2]; char3 = (int) bytearr[count-1]; if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) throw new UTFDataFormatException( "malformed input around byte " + (count-1)); chararr[chararr_count++]=(char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; default: /* 10xx xxxx, 1111 xxxx */ throw new UTFDataFormatException( "malformed input around byte " + count); } } // The number of chars produced may be less than utflen return new String(chararr, 0, chararr_count); }}
对应的DataOutputStream源码如下,writeXXX()与DataInputStream中的readXXX()与之对应,如下:
package java.io;public class DataOutputStream extends FilterOutputStream implements DataOutput { public synchronized void write(int b) throws IOException { out.write(b); incCount(1); } 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); } public final void writeShort(int v) throws IOException { out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(2); } public final void writeChar(int v) throws IOException { out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(2); } 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); } 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); } public final void writeFloat(float v) throws IOException { writeInt(Float.floatToIntBits(v)); } public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); } 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); } 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); } public final void writeUTF(String str) throws IOException { writeUTF(str, this); } static int writeUTF(String str, DataOutput out) throws IOException { int strlen = str.length(); int utflen = 0; int c, count = 0; /* 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) throw new UTFDataFormatException( "encoded string too long: " + utflen + " bytes"); byte[] bytearr = null; if (out instanceof DataOutputStream) { DataOutputStream dos = (DataOutputStream)out; if(dos.bytearr == null || (dos.bytearr.length < (utflen+2))) dos.bytearr = new byte[(utflen*2) + 2]; bytearr = dos.bytearr; } else { bytearr = new byte[utflen+2]; } bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); 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); if ((c >= 0x0001) && (c <= 0x007F)) { bytearr[count++] = (byte) c; } else if (c > 0x07FF) { bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } out.write(bytearr, 0, utflen+2); return utflen + 2; } public final int size() { return written; }}
- Java IO 其他流 -- 字节数组流,字符数组流和数据流
- IO流之字节数组流和字符串流
- 6.Java IO:字节和字符数组
- 黑马程序员-----IO之字节数组流、字符数组流和字符串流
- IO流第十一课,其他流,字节数组流
- java 对象流、管道流、基本数据流、字节数组流
- Java复习之IO字节数组流
- 黑马程序员_10字节流、字符流、转换流、控制流、数据流、字节数组流
- Java之数据流DataInput(Output)Stream 和 字节数组流 ByteArrayInput(Output) Stream的嵌套
- Java IO字符流和字节流
- Java-IO:字节流和字符流
- Java-IO-字节流和字符流
- java IO字符流和字节流
- IO流 数据流 DataInputStream DataOutputStream ByteArrayIntOutputStream字节数组 Math.randonm()方法
- Java I/O之数据流字符串流 Java I/O之数据流字符串&&字节数组流 DataInputStream :数据输入流允许程序以与机器无关方式从底层&&字节数组流
- 黑马程序员——Java基础---IO(二)---对象流、管道流、RandomAccessFile、数据流、数组流、字符编码
- Java流编程实例之三--字节数组流和字符数组流
- 字符IO&数据流IO&对象流IO
- 数据结构实验之查找四:二分查找
- 网上干货 ElasticSearch详解与优化设计
- WebSocket网络接口
- FILE文件流的中fopen、fread、fseek、fclose的使用
- 音视频开发——流媒体数据传输RTP(三)
- Java IO 其他流 -- 字节数组流,字符数组流和数据流
- 关于页面刷新的那些事
- 暑期dp46道(19)HDU 1078 FatMouse and Cheese dfs+记忆化搜索
- spring中c3p0配置
- Linux文件系统十问,你知道吗?
- 用户空间与内核空间,进程上下文与中断上下文[总结]
- JAVA第一个小项目——五子棋的制作(第一篇)
- TortoiseSVN小乌龟,忽略Android studio的一些文件和文件夹
- Android深究之序列化