数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流。

    “流是磁盘或其它外围设备中存储的数据的源点或终点。” 

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));
}
} 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:欢迎各位吐槽指点~

最新文章

  1. Yii2 定时任务创建(Console 任务)
  2. PHP操作Mongodb之高级查询篇
  3. Eclipse 反编译器
  4. STM32F4_RCC系统时钟配置及描述
  5. NYOJ 题目15 括号匹配(二)(区间DP)
  6. ###g++编译器
  7. [Oracle]日期和毫秒转换(Date-&gt;int)
  8. 《javascript语言精粹》——第4章函数
  9. Python自学笔记-递归函数(来自廖雪峰的官网Python3)
  10. Hessian探究(一)Hessian与springMVC结合
  11. SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
  12. Android音频系统
  13. JAVA期末考试整理
  14. javax.servlet-api 和 servlet-api 区别
  15. Mysql临时文件目录控制
  16. Android_学习系列(33)--App应用之提交到各大市场渠道
  17. Passbook
  18. 3种启动tornado的方式
  19. clear(), evict(), flush()三种方法的用法实例
  20. 关于UITableView 不能回调 tableView: cellForRowAtIndexPath的问题

热门文章

  1. Spring-Framework 源码阅读之@Autowired和AutowiredAnnotationBeanPostProcessor
  2. python 排序sorted
  3. Warning[w2]: Symbol ?P…
  4. CI 结合 vue.js 的搜索功能模块
  5. 用Python处理实验数据
  6. java出现The type java.lang.Object cannot be resolved. It is indirectly referenced.....解决办法
  7. mysql获取当前时间,前一天,后一天
  8. Spring 并发访问的线程安全性问题
  9. JAVAOO零碎--内存叠加
  10. 三种Join方法