自己动手写java 字节流输入输出流
2017-09-07 20:14
459 查看
自己动手写java 字节流输入输出流
数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流。“流是磁盘或其它外围设备中存储的数据的源点或终点。”
1) 数据流:
一组有序,有起点和终点的字节的数据序列。包括输入流和输出流。
2) 输入流(Input Stream):
程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道
3) 输出流:
程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。
采用数据流的目的就是使得输出输入独立于设备。
1.输入流代码
import java.io.IOException; import java.io.InputStream; //字节数组输入流 public class MyByteInputStream extends InputStream { private byte[] buf;// 存放数据流的数组 private int bufLength;// 记录buf数组长度 private int pos;// 已经存放数据的下标位置 private int readPos = 0;// 记录当前数据流读取到的位置 public MyByteInputStream(int i) { buf = new byte[32]; bufLength = 32; pos = 0; } // 构建输入流(直接存入待输入的流数据) public MyByteInputStream(byte[] b) { if (b != null && b.length > 0) { int copyLength = b.length; buf = new byte[copyLength]; System.arraycopy(b, 0, buf, 0, copyLength);// 复制数组内容 bufLength = copyLength; pos = copyLength; } else { buf = new byte[32]; bufLength = 32; pos = 0; } } /* * 若有数据则返回对应buf[readPos],否则返回-1 */ public int read() throws IOException { if (pos > 0 && readPos <= (pos - 1)) { readPos = readPos + 1; return buf[readPos - 1]; } return -1; } }
2.输出流代码
import java.io.OutputStream; public class MyByteOutStream extends OutputStream { private byte[] buf;// 输出流 private int length;// 存放输出流的长度 private int pos;// 写到的位置 public MyByteOutStream() { buf = new byte[32]; length = 32; pos = 0; } public MyByteOutStream(int size) { if (size > 0) { buf = new byte[size]; length = size; pos = 0; } else { buf = new byte[32]; length = 32; pos = 0; } } /** * 将字符b写入到字节流中,若流空间不够则扩展 * * @param b */ public void write(int b) { if (pos < length) { buf[pos] = (byte) b; pos = pos + 1; } else { // TODO:扩展字节流buf[]大小 } } /** * 将输出流copy * * @return */ public byte[] toByteArray() { if (pos > 0) { byte[] b = new byte[pos]; System.arraycopy(buf, 0, b, 0, pos); return b; } return null; } }
3.测试类
import java.io.IOException; public class MyTest { public static void main(String[] args) { String inputStr = "Test input stream!"; String outStr = "Test out strem!"; // 自定义输入流 MyByteInputStream myByteInputStream = new MyByteInputStream(inputStr.getBytes()); try { for (int i; (i = myByteInputStream.read()) != -1;) { System.out.print(Character.toString((char) i)); } ab79 } catch (IOException e) { e.printStackTrace(); } System.out.println(""); // 自定义输出流 MyByteOutStream myByteOutStream = new MyByteOutStream(100); byte[] b = outStr.getBytes(); for (int i = 0; i < b.length; i++) { myByteOutStream.write(b[i]); } byte[] outb = myByteOutStream.toByteArray(); for (int i = 0; i < outb.length; i++) { System.out.print(Character.toString((char) outb[i])); } System.out.println(""); } }
ps:欢迎各位吐槽指点~
相关文章推荐
- 自己动手写java 字节流输入输出流
- Java之字节流(文件输入、输出流)
- Java学习笔记之输入输出流(一) File、文件字节流、文件字符流
- Java语言基础-IO流(输入输出流) 字节流、转换流
- Java笔记(7)-输入、输出流、File类、InputStream、Reader、文件字节流、文件字符流、缓冲流、随机流、数据流、对象流、序列化和对象克隆、Scanner解析文件、Console流
- JAVA输入/输出流(字节流、字符流、缓冲流)
- java中的输入输出流(字节流 字符流 节点流 过滤流)
- java输入输出流,字节流,字符流
- java中的io流(输入输出流,序列化与反序列化,字节流 , 字符流,包装流,缓冲流)
- Java输入输出流-----字节流(1)
- JAVA基础 day21 字节流的输入和输出 字节流的缓冲区 (自己实现BufferedInputStream) 键盘输入 转换流的用法 改变标准输入输出设备 IO流操作的基本规律
- JAVA I/O操作那些事之输入输出流简介(一)
- 自己动手编写启动JAVA程序的EXE文件
- Java学习笔记之IO(四):BufferedInputStream缓冲输入字节流
- 自己动手写搜索引擎(常搜吧历程四#分词#)(Java、Lucene、hadoop)
- Java 输入输出流
- Java自己动手写连接池二
- 自己做的demo---关于java控制台输入跟类型转化跟处理异常的demo
- Java输入输出流
- Java——IO(输入输出流)