JAVA学习第五十七课 — IO流(十一)
一、管道流
PipedInputStream 和 PipedOutPutStream
输入和输出能够直接进行连接。结合线程使用
管道流,顾名思义,写一个读一个。连成一个管子
API文档:管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的全部数据字节。通常,数据由某个线程从
PipedInputStream 对象读取,并由其它线程将其写入到对应的 PipedOutputStream。
不建议对这两个对象尝试使用单个线程。由于这样可能死锁线程。
管道输入流包括一个缓冲区。可在缓冲区限定的范围内将读操作和写操作分离开。
假设向连接管道输出流提供数据字节的线程不再存在。则觉得该管道已损坏
class Input implements Runnable{
private PipedInputStream pis;
Input(PipedInputStream pis){
this.pis = pis;
}
@Override
public void run() {
try {
byte[] by = new byte[1024];
int len = pis.read(by);
String str = new String(by,0,len);
System.out.println("str = "+str);
pis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
class Output implements Runnable{
private PipedOutputStream pos;
Output(PipedOutputStream pos) {
// TODO Auto-generated constructor stub
this.pos = pos;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
pos.write("你好".getBytes());//字符串转字节流
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class Main {
public static void main(String[] args)throws IOException{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);//两流对接
Thread t1 = new Thread(new Input(pis));
Thread t2 = new Thread(new Output(pos));
t1.start();
t2.start();
}
}
管道流应用不是非常多,可是其自身非常有特点
IO包中的其它类
操作基本类型数据的流对象—DataInputStream与DataOutputStream
操作字节数组—ByteArrayInputStream与ByteArrayOutputStream
操作字符数组—CharArrayInputStream与CharArrayOutoutStream
操作字符串—StringWriter与StringReader
DataInputStream与DataOutputStream
public static void read() throws IOException {
// TODO Auto-generated method stub
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
public static void DataStreamDemo() throws IOException {
// TODO Auto-generated method stub
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("你好胖");以与机器无关方式使用 <a target=_blank href="">UTF-8 改动版</a>编码将一个字符串写入基础输出流。
dos.close();
}
剩下的方法和OutputStream、InputStream差点儿相同
操作数组的流
源和汇都是内存。ByteArrayInputStream、ByteArrayOutputStream
ByteArrayOutputStream:此类实现了一个输出流,当中的数据被写入一个
byte 数组。缓冲区会随着数据的不断写入而自己主动增长。可使用 toByteArray() 和 toString() 获取数据,关闭
ByteArrayOutputStream 无效。
public static void ByteStreamDemo() throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream("asdfd".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while((len = bis.read())!=-1){
bos.write(len);
}
System.out.println(bos.toString());
}
剩下的流使用方法和ByteArrayInputStream都是同样的
编码问题
编码问题无非就是文字和二进制之间的相互转换
字符串->字符数组:编码
字符数组->字符串:解码
public static void Demo() {
String str = "你好";
//编码
byte[] by = str.getBytes();
for(byte b : by){
System.out.println(b);
}
//解码
String str1 = new String(by);// == new String(by,"GBK");
System.out.println(str1);
}
编码编对了,有可能能成功解码
public static void main(String[] args)throws IOException{
String str = "ab你好asdas撒旦发射的";
int len = str.getBytes("GBK").length;
for(int i = 0;i<len;i++){//依照i个字节取
System.out.println("OK : "+Demo(str,i+1));
}
}
public static String Demo(String str,int len) throws IOException {
byte[] by = str.getBytes("GBK");
int count = 0;//记录从哪个索引開始,字节不是汉字
for(int i = len-1;i>=0;i-- ){
if(by[i]<0){
count++;
}else {
break;
}
}
//汉字相应2个字节,汉字的个数 = 有负字节数/2
return (count & 1) == 0?new String(by,0,len):new String(by,0,len-1);
}
JAVA学习第五十七课 — IO流(十一)的更多相关文章
- JAVA学习第四十七课 — IO流(一):文件的读写
输入流和输出流相对于内存 将外部设备的数据读取到内存中:输入 将内存中的数据写入外部设备中:输出 IO流经常使用基类 字节流的抽象基类:InputStream,OutputStream 字符的抽象基类 ...
- JavaSE学习总结(十七)—— IO流
一.IO流概要 1.1.概念 开发中经常要进行输入输出操作,掌握Java中的IO流显得非常必要. 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流, ...
- java学习第21天(IO流的使用)
IO流分类: A:流向 输入流 读取数据 输出流 写出数据 B:数据类型 字节流 字节输入流 字节输出流 字符流 字符输入流 字符输出流 注意: a:如果我们没有明确说明按照什么分,默认按照数据类型分 ...
- 【java学习笔记】文件读写(IO流)
1.字节流 FileInputStream.FileOutputStream ①FileInputStream import java.io.FileInputStream; public class ...
- java学习第20天(IO流)
构造方法File file = new File("e:\\demo"); 创建文件夹 File file = new File("e:\\demo"); fi ...
- Python学习第五堂课
Python学习第五堂课推荐电影:华尔街之狼 被拯救的姜哥 阿甘正传 辛德勒的名单 肖申克的救赎 上帝之城 焦土之城 绝美之城 #上节内容: 变量 if else 注释 # ""& ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第五周
java学习第五周心得体会 本周,是Java学习第五周,随着时间推移,随着课本内容的推进,我们接触到的程序也开始变得越来越复杂,不再是二三章那些用来练手的小程序了,这一点,在我们的例题运 ...
- JAVA第09次实验(IO流)
JAVA第09次实验(IO流) 0.字节流与二进制文件 我的代码 import java.io.DataInputStream; import java.io.DataOutputStream; im ...
- java基础第十五篇之IO流和递归算法
FileInputStream : 输入流 int available() : 一次读取所有的字节数 read() : 将文件上的字节读取到内存的数组中 FileOutputStream : 输出流 ...
随机推荐
- Autodesk Stingray 游戏引擎
Autodesk的游戏引擎质量够高的. http://v.youku.com/v_show/id_XMTMwMjc0MDIwMA==.html?qq-pf-to=pcqq.group http://v ...
- Linker scripts之MEMORY
1 MEMORY command The MEMORY command describes the location and size of blocks of memory in the targe ...
- BIOS启动项中的设备都有哪些
Floppy 软式磁盘驱动器,简称FDD,也就是我们平时所说的软驱. CD-ROM 不用多说了,大家都知道,这是光盘驱动器,也就是我们平时说得光驱. SCSI SCSI的全名是:Small Compu ...
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...
- php--opp--1.什么是面向对象?
面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成 ...
- SharePoint咨询师之路:设计之前的那些事二:规模
提示:本系列只是一个学习笔记系列,大部分内容都可以从微软官方网站找到,本人只是按照自己的学习路径来学习和呈现这些知识. 有些内容是自己的经验和积 累,如果有不当之处,请指正. 咨询师更多的时候是解决方 ...
- C++11 语法记录
转自:http://blog.csdn.net/crayondeng/article/details/18563121 一.Lambda表达式 C++ 11中的Lambda表达式用于定义并创建匿名的函 ...
- C++ 16进制转10进制
#include <stdio.h>#include <string.h>unsigned long f(char* str){ unsigned long var=0; un ...
- C#中字符串与byte[]相互转换
字符串转换为byte[] 给定一个string,转换为byte[],有以下几种方法. 方法1: static byte[] GetBytes(string str) { byte[] bytes = ...
- 研究QGIS二次开发笔记(一)
为了在QT程序中嵌入一个地图,最终选择了QGIS来干这件事.选型阶段真是呵呵.我折腾的是QGIS2.4.0. 首先,到官方网站下载安装QGIS.如果你跟我一样懒的话,可能希望下载一个已经编译好的win ...