-
Java连载156-IO总结(三)
一、管道流
final PipedOutputStream pps = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(pps);
new Thread(new Runnable() {
public void run() {
try {
pps.write("厉害了".getBytes(StandardCharsets.UTF_8));
pps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
int b3 = 0;
byte[] flush = new byte[1024];
while ((b3=pis.read(flush)) != -1) {
System.out.println(new String(flush, 0, flush.length));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
-
上面的管道流和Linux中的管道流是类似的
二、基本数据流
-
可以使用DataInputStream等及逆行基本数据的存储
String address = "E:\\d05_gitcode\\Java\\newJava\\src\\com\\newJava\\newFile.txt";
DataInputStream dis = new DataInputStream(new FileInputStream(address));
byte b = dis.readByte();
System.out.println(b);
short s = dis.readShort();
System.out.println(s);
int i = dis.readInt();
System.out.println(i);
long l = dis.readLong();
System.out.println(l);
float f = dis.readFloat();
System.out.println(f);
double d = dis.readDouble();
System.out.println(d);
char c = dis.readChar();
System.out.println(c);
boolean bo = dis.readBoolean();
System.out.println(bo);
DataOutputStream dos = new DataOutputStream(new FileOutputStream(address));
dos.writeByte(1);
dos.writeShort(2);
dos.writeInt(3);
dos.writeLong(4L);
dos.writeFloat(5.0f);
dos.writeDouble(6.0d);
dos.writeChar(7);
dos.writeBoolean(false);

三、缓存流
-
CPU运行速度是内存的几百倍,是磁盘的几百万倍,因此减少和磁盘的交互,就能提高IO的速度,我们在这两者之间提供一个buffer,缓存流(也就是BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter), 来增多一次性读写的数量,减少IO的次数。
四、打印流
-
我们所使用的System.out.println()方法就是打印流的一种
StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw)) {
pw.println("lisudfos");
}
System.out.println("lisudfos");
五、源码
-
github路径:https://github.com/ruigege66/Java/blob/master/newJava/src/com/newJava -
CSDN:https://blog.csdn.net/weixin_44630050 -
博客园:https://www.cnblogs.com/ruigege0000/
原文:https://www.cnblogs.com/ruigege0000/p/15734697.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比