I/O流分类:

InputStream和Read的子类都有read(),用来读取单个字节或字节数组

OutputStream和write的子类都有write(),用来写入单个字节或字节数组

一般都是通过装饰器Decorator模式叠加多个对象提供所期望的功能。创建单一的流,却需要创建多个对象

InputStream:

InputStream为字节输入流,一般都是通过其子类实现功能,是所有字节输入流的基类

public abstract class InputStream implements Closeable {
private static final int MAX_SKIP_BUFFER_SIZE = 2048; public abstract int read() throws IOException; //读取1byte的数据,返回int类型。若返回值=-1说明没有读取到任何字节 public int read(byte[] var1) throws IOException {} //读取var1.length个字节的数据放到var1数组中,返回值是读取的字节数 public int read(byte[] var1, int off, int len) throws IOException {} //从输入流中最多读取len个字节的数据,存放到偏移量为off的var1数组中。 public long skip(long var1) throws IOException {} //忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取 public int available() throws IOException {} //返回输入流中可以读取的字节数。必须是InputStream的子类调用才行,本身调用返回0 public void close() throws IOException {} //每次读取结束,都要关闭输入流并释放与流相关的资源 }

OutputStream:

OutputStream为字节输出流,一般都是通过其子类实现功能,是所有字节输出流的基类

public abstract class OutputStream implements Closeable, Flushable {
public OutputStream() {} public abstract void write(int var1) throws IOException; //先将int转换为byte类型,把低字节写入到输出流中 public void write(byte[] var1) throws IOException {} //将数组var1中的字节写到输出流 public void write(byte[] var1, int off, int len) throws IOException {} //将数组var1的字节从偏移量off开始的len个字节写到输出流 public void flush() throws IOException { } //将数据缓冲区中数据全部输出,并清空缓冲区 public void close() throws IOException { } //每次写入结束,都要关闭输出流并释放与流相关的资源
}

FileInputStream和FileOutputStream:

public static void main(String[] args) throws IOException {
String pathName = "D:" + File.separator + "a.txt";
File file = new File(pathName);
OutputStream outputStream = new FileOutputStream(file);
outputStream.write("abc1".getBytes());
outputStream.close();
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int length = inputStream.read(bytes);
String s = new String(bytes, 0, length);
System.out.println(s);
inputStream.close();
}

输出结果:

我是好人追加内容

FileOutputStream用来从内存区读数据到文件,如果文件存在,会把内容覆盖,如果不存在,创建文件,然后把数据写入

FileInputStream用来从文件读数据到内存区

ByteArrayInputStream和ByteArrayOutputStream:

ByteArrayOutputStream创建的时候,程序内部创建一个byte[]类型的缓冲区,然后向数组中写入或读出byte型数据。

public static void main(String[] args) throws IOException {
OutputStream outputStream = new ByteArrayOutputStream();
outputStream.write("abc".getBytes());
byte[] bytes = ((ByteArrayOutputStream) outputStream).toByteArray();
outputStream.close();
InputStream inputStream = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len;
while ((len = inputStream.read(bytes1)) != -1) {
System.out.println(new String(bytes1, 0, len));
}
}

输出结果:abc

可以从得到byte[]缓冲区,然后读取数据,可以向缓冲区多次写入数据,然后集中一次读取

BufferedInputStream、BufferedInputStream:

public static void main(String[] args) throws IOException {
String pathName = "D:" + File.separator + "a.txt";
File file = new File(pathName); BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File("D:" + File.separator + "b.txt"))); byte[] bytes = new byte[4];
while (inputStream.read(bytes) != -1) {
System.out.println(new String(bytes));
outputStream.write(bytes);
}
inputStream.close();
outputStream.close();
}

在文件流外面包装一层缓冲流读取函数,先从缓冲区读取数据

PS:在写入缓冲区结束之后,一定要调用flush()或者close(),否则数据一致存在缓冲区,而没有写出

PipedInputStream、PipedOutputStream:

  管道流主要用于连接两个线程的数据通信.通过连接而产生一个通信管道,PipedOutputStream向管道中写入数据,PipedInputStream从管

道中读取数据。管道输出流和管道输入流执行时不能互相阻塞,所以一般要开启独立线程分别执行

public class Sender implements Runnable{
private PipedOutputStream outputStream = new PipedOutputStream();
public PipedOutputStream outputStream() {
return this.outputStream;
}
@Override
public void run() {
try {
outputStream.write("message".getBytes());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Receiver implements Runnable{
private PipedInputStream inputStream = new PipedInputStream();
public PipedInputStream inputStream() {
return this.inputStream;
} @Override
public void run() {
byte[] bytes = new byte[1024];
try {
int len = inputStream.read(bytes);
if (len != -1) {
System.out.println("成功接收:" + new String(bytes));
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) throws IOException {
Sender sender = new Sender();
Receiver receiver = new Receiver();
PipedOutputStream outputStream = sender.outputStream();
PipedInputStream inputStream = receiver.inputStream();
inputStream.connect(outputStream);
new Thread(sender).start();
new Thread(receiver).start();
}
}

输出结果:

成功接收:message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

ObjectInputStream、ObjectOutputStream:

ObjectOutputStream可以把对象直接存入到文件中,然后利用ObjectInputStream读取文件还原成对象

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable{ private int id;
private String name;
private int sex; @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public static void main(String[] args) throws Exception {
String pathName = "D:" + File.separator + "a.txt";
File file = new File(pathName);
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
Student student = new Student(1001, "sam", 1);
outputStream.writeObject(student);
outputStream.close(); ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
Student student1 = (Student)inputStream.readObject();
System.out.println(student1);
}

PS:保存的对象必须实现了Serializable接口,否则出现异常:java.io.NotSerializableException: com.it.entity.Student

最新文章

  1. WIN7 共享网络方法
  2. IE11如何采用其他低级版本调试网页
  3. C#运算符之与,或,异或及移位运算
  4. 【python】【转】if else 和 elif
  5. C#socket通信1
  6. CentOS x 64 MooseFS 学习
  7. canvas作图
  8. Vulkan Tutorial 12 Fixed functions
  9. 微信小程序之最简单的Demo设计使用
  10. Activity简单几步支持向右滑动返回
  11. OC实现带弹跳动画按钮的界面控制器view
  12. json2mysql
  13. C# 获取Newtonsoft.Json的JObject多层节点内容
  14. 多线程与CPU和多线程与GIL
  15. [JLOI2016/SHOI2016]侦察守卫(树形dp)
  16. 【1】【leetcode-99】 恢复二叉搜索树
  17. adduser Ubuntu添加sudo用户
  18. [javascript] 看知乎学习js事件触发过程
  19. Java——多线程小例子
  20. POJ 2367:Genealogical tree(拓扑排序模板)

热门文章

  1. AnimatorCompatHelper clearInterpolator
  2. 并不对劲的字符串专题(二):kmp
  3. Servlet启动运行顺序
  4. Bootstrap中的字体图标
  5. MySQL基础操作——转
  6. 洛谷 P3825 [NOI2017]游戏 【2-SAT+状压】
  7. 第四代增强 NEW BADI的定义及实现
  8. 3-2 -------WERTYU------题挺水的
  9. javascript---DOM大编程
  10. 51nod 1154 回文串划分