Java BIO
来源:互联网 发布:matlab离散数据拟合 编辑:程序博客网 时间:2024/05/05 02:42
BIO是出现得比较陈旧的IO技术了,它的缺点在于并发量高时,IO操作等待的时间会比较长,因为它是阻塞IO。在IO中,一般按照数据的不同,可以分为字节流和字符流,字节流把读到的数据当做字节来处理,而字符流把数据当做字符来处理。
字节流
根据输入和输出,字节流又可以分为输入字节流InputStream和输出字节流OutputStream。
输入字节流InputSteam
int read();//每次从字节流当前位置中读入一个字节int read(byte[] buffer);//从字节流中当前位置开始读取数据今日到数值buffer中,最多读取buffer.length个字节int read(byte[] buffer, int off, int len);//从字节流当前位置开指定的偏移off开始,读取len个字节到数组buffer中int available();//返回字节流中可以读取的字节数long skip(long a);//从字节流当前位置开始,跳过a个字节void close();//关闭字节输入流
该接口还有许多实现类
- ByteArrayInputStream
ByteArrayInputStream(byte[] src);ByteArrayInputStream(byte[] src, int off, int len);
两个构造器都需要一个byte[]作为源。这个类可以直接操作byte[]。
public static void main(String[] args) {// TODO Auto-generated method stubbyte[] src = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};ByteArrayInputStream is = new ByteArrayInputStream(src);while(is.available() > 0){int num = is.read();System.out.print(num);}try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
- FileInputStream
FileInputStream(File file);
这个实现类用得是相当多,因为文件IO其实用得是非常多的。
public static void main(String[] args) {// TODO Auto-generated method stubFile file = new File("test.txt");if(file.exists()){try {FileInputStream is = new FileInputStream(file);byte[] buffer = new byte[1024];//缓冲区int len = -1;while((len = is.read(buffer, 0, buffer.length)) != -1){System.out.println(new String(buffer, 0 , len));}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
- ObjectInputStream
ObjectInputStream(InputStream is);
通常来说,一个序列化的对象被写到了磁盘上,也算是一个文件了,所以通常的InputStream会采用其子类FileInputStream作为实际的参数。
public static void main(String[] args) {// TODO Auto-generated method stubFile file = new File("person.txt");if(file.exists()){FileInputStream is1;try {is1 = new FileInputStream(file);ObjectInputStream is2 = new ObjectInputStream(is1);Object obj = is2.readObject();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
- StringBufferInputStream
这个类使用字符创String作为源,它的构造器如下
StringBufferInputStream(String src);这个类已经过时了,不推荐使用。
public static void main(String[] args) {// TODO Auto-generated method stubString src = "hello,world";StringBufferInputStream is = new StringBufferInputStream(src);byte[] buffer = new byte[is.available()];try {is.read(buffer);System.out.println(new String(buffer));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
- PipedInputStream
管道输入流,该输入流需要从管道处获取数据。它的构造器如下
PipedInputStream(PipedOutputStream os);PipedInputStream();
第一个构造器将使用管道输出流作为源,第二个构造器将创建一个无源的输入流。
- SequenceInputStream
该类可以将两个个InputStream串联起来。它的构造器如下
SequenceInputStream(InputStream is1, InputStream is2);SequenceInputStream(Enumeration<? extends InputStream> e);
public static void main(String[] args) {// TODO Auto-generated method stubtry {FileInputStream is1 = new FileInputStream(new File("src/a.txt"));FileInputStream is2 = new FileInputStream(new File("src/b.txt"));FileInputStream is3 = new FileInputStream(new File("src/c.txt"));Vector<InputStream> v = new Vector<InputStream>();v.add(is1);v.add(is2);v.add(is3);SequenceInputStream sis = new SequenceInputStream(v.elements());System.out.println(sis.available());int i = -1;while((i = sis.read()) != -1){System.out.print((char)i);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
注意,这里不能使用available()来判断流的可用数据,而是需要判断是否到流的尾部read()是否==-1,才能将合并后的数据全部输出来。
- 过滤输入流FilterInputStream
它把其他的流作为基本数据源,FilterInputStream会将所有请求传递给包含InputStream的所有方法。这个类本身对于数据流没有什么用,它的子类DataInputStream和PushBackInputStream倒是蛮有用的。
DataInputStream
这个类可以将一些字节数据读取出来并转化成为基本的Java类型
public static void main(String[] args) throws IOException {// TODO Auto-generated method stubbyte[] src = new byte[]{1,1};ByteArrayInputStream is = new ByteArrayInputStream(src);DataInputStream dis = new DataInputStream(is);short s = dis.readShort();System.out.println(s);}PushBackInputStream
这个类可以有推回或者取消读取一个字节的能力,使用unread()方法可以让游标重新回到上一个读取过了的字节处。
字节输出流OutputStream
字节输入流负责字节流的取得,而字节输出流则负责字节的输出。
OutputStream是一个接口,它有着以下的一些实现类:
ByteArrayOutputStream
FileOutputStream
StringBufferOutputStream
ObjectOutputStream
此外,还有特殊的实现
PipedOutputStream
SequenceOutputStream
FilterOutputStream
- ByteArrayOutputStream
该类可以将字节流输出到字节数组里面
- FileOutputStream
该类可以将字节流输出到文件里面
- StringBufferOutputStream
该类可以将字节流输出到字符串里面
- ObjectOutputStream
该类可以将序列化对象输出
字符流
字符输入流Reader
int read(char[] chs, int off, int len);//从字符流当前位置开始,将字符流读入到字符数组chs中,从off偏移位置开始存放,读入的字符流长度为lenint read();//从字符流当前位置开始,一次读取一个字符int read(char[] chs);//从字符流当前位置开始,将字符流读入存放到chs字符数组中boolean ready();//判断该流是否准备好void reset();//重置流的位置long skip(long n);//从字符流当前位置开始,跳过n个字符void mark(int n);//对流进行标记void colse();//关闭流
- CharArrayReader
CharArrayReader(char[] src);CharArrayReader(char[] src, int off, int len);
public static void main(String[] args) throws IOException{// TODO Auto-generated method stubchar[] src = new char[]{'h', 'e', 'l', 'l', 'o'};CharArrayReader reader = new CharArrayReader(src);int i = -1;while((i = reader.read()) != -1){System.out.print((char) i);}}
- StringReader
StringReader(String src);
public static void main(String[] args) throws IOException{// TODO Auto-generated method stubString src = new String("hello");StringReader reader = new StringReader(src);int i = -1;while( (i = reader.read()) != -1){System.out.print((char) i);}}
InputStreamReader
InputStreamReader(InputStream is);
public static void main(String[] args) throws IOException{// TODO Auto-generated method stubFile file = new File("src/a.txt");if(file.exists()){FileInputStream is = new FileInputStream(file);InputStreamReader reader = new InputStreamReader(is);int i = -1;while( (i = reader.read()) != -1){System.out.print((char) i);}}}
适合用于读取一些全是字符的文件。这个类还有一个子类FileReader,FileReader是以文件作为源,从文件中读取字符流的,其构造器
FileReader(String filename);FileReader(File file);
- PipedReader
PipedReader(PipedWriter writer);
- BufferReader
BufferedReader(Reader r);BufferedReader(Reader r, int size);
public static void main(String[] args) throws IOException{// TODO Auto-generated method stubFile file = new File("src/a.txt");if(file.exists()){FileInputStream is = new FileInputStream(file);InputStreamReader reader = new InputStreamReader(is);BufferedReader r = new BufferedReader(reader);int i = -1;while( (i = r.read()) != -1){System.out.print((char) i);}}}
- FilterReader
字符输出流Writer
write(String str);write(String str, int off, int len);write(char[] chs);write(char[] chs, int off, int len);write(int i);void flush();void close();
其实现类如下:
- CharArrayWriter
- StringWriter
- OutputStreamWriter
OutputStreamWriter(OutputStream os);
FileWriter(String filename);FileWriter(File file);
- PrintWriter
PrintWriter(OutputStream os)
- PushBackWriter
该类可以回退一个字符。
标准输入输出流
标准输入流System.in
标准输出流System.out
字节流转换流InputStreamReader和OutputStreamWriter
- Java BIO
- JAVA-BIO
- Java BIO
- 【java】java BIO
- java BIO NIO AIO
- Java BIO、NIO、AIO
- java BIO NIO AIO
- Java BIO 服务器
- java bio & nio
- Java BIO/NIO/AIO
- java bio nio aio
- java nio aio bio
- java nio bio aio
- JAVA AIO BIO NIO
- Java BIO、NIO、AIO
- Java bio nio aio
- Java BIO、NIO、AIO
- JAVA BIO例子
- Oracle中job的使用详解
- 卸载oracle数据库
- C#利用委托进行多线程访问控件
- php +MySQL +KSWEB Android 应用
- 第十七周项目1-数据结构拓展之数组操作
- Java BIO
- 数据结构——算法之(029)( 字符串原地压缩)
- linux类系统访问windwos共享文件
- 递归
- JUnit使用Eclipse建立Test Suite - 就是爱Java
- win8的使用以及win8开发环境
- json解析详解
- P31RestKit.dll 2.0 Unity3d json
- 图吧丛卉:大数据改变车主“命运”