码迷,mamicode.com
首页 > 编程语言 > 详细

Java之IO流---字节流

时间:2016-09-29 13:47:16      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

1.概述

1.1 IO流的引入

IO流在很多语言已有体现,诸如C语言的stdio.h,C++中的iostream。Java中的IO流大抵是用于在控制台、磁盘、内存上进行数据的读写操作,完成数据的传递。

我们可以对它进行如下分类:

  • 按处理的数据类型可分为字节流字符流
  • 按流的流向可分为输入流(in)与输出流(out)
  • 按流的功能可分为节点流(Node)和过滤流(Filter)

本篇侧重于梳理字节流相关的知识,毕竟作为字符流的前辈,它还是非常重要的。下篇继续梳理字符流。

1.2 IO流的继承体系图

大概描述了流的两部分与他们的继承关系。本来想从网上copy一张图的,最后还是决定自己画一张,加深理解!
技术分享

2.字节流

在Java中,字节流一般适用于处理字节数据(诸如图片、视频),字符流适用于处理字符数据(诸如文本文件),但二者并没有严格的功能划分,因为有转换流的存在,使得对于数据的处理变得更加灵活。
InputStream和OutputStream分别是字节输入流与字节输出流的基类,它们的子类都是字节流,主要用在按字节来处理二进制数据。

下面会从 InputStream和OutputStream这两个基类来说起,毕竟先有父再有子。它们的子类既有共性,也有各自的出彩之处,此所谓龙生九子,各有千秋。

2.1 InputStream

此抽象类是表示字节输入流的所有类的超类。

需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法(read()方法)。

2.11方法摘要

大概定义了读写等操作,供所有子类来实现这些共性操作。实现了此类的子类必须实现read() 方法 (从下面就可以看出,此方法是抽象方法)

 int available() 
          返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。 
 void close() 
          关闭此输入流并释放与该流关联的所有系统资源。 
 void mark(int readlimit) 
          在此输入流中标记当前的位置。 
 boolean markSupported() 
          测试此输入流是否支持 mark 和 reset 方法。 
 abstract  int read() 
          从输入流中读取数据的下一个字节。 
 int read(byte[] b) 
          从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。 
 int read(byte[] b, int off, int len) 
          将输入流中最多 len 个数据字节读入 byte 数组。 
 void reset() 
          将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。 
 long skip(long n) 
          跳过和丢弃此输入流中数据的 n 个字节。 

从下面开始梳理InputStream的子类的用法。

2.12 FileInputStream

用于读取文件系统的文件的输入流。

  • FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
  • FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。

2.12.1 构造方法摘要

FileInputStream(File file) 
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。 
FileInputStream(FileDescriptor fdObj) 
          通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统中某个实际文件的现有连接。 
FileInputStream(String name) 
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。 

2.12.2方法摘要

仅列出了区别于基类的方法

 FileChannel getChannel() 
          返回与此文件输入流有关的唯一 FileChannel 对象。 
 FileDescriptor getFD() 
          返回表示到文件系统中实际文件的连接的 FileDescriptor 对象,该文件系统正被此 FileInputStream 使用。 

2.12.3 使用示例

1.使用步骤:

①关联一个文件以得到一个输入流
②进行读取操作
③关闭流

2. 下面代码主要内容是创建文件输入流的几种方式以及几种read方法的使用,后面会讲解通过BufferedInputStream的read方法来高效读取
注意:用字节流读取中文显示到控制台会出现乱码的情况,是因为汉字占两个字节,而此流每次只能读一个字节,就把它转为了字符。写入文本文件不会出现此情况

public class FileInputStreamReview {
    public static void main(String[] args) {
        //test1();
        test2();

    }

    /**
     * 文件输入流的创建方式以及字节缓冲流
     */
        private static void test2() throws IOException {
//      FileInputStream fis=new FileInputStream(new File("jar.txt")); // 1
        FileInputStream fis=new FileInputStream("jar.txt");  // 2
        BufferedInputStream bis=new BufferedInputStream(fis);
        // ① 读一个字节数组 
//          byte[] buff =new byte[2*1024];
//          int len=-1;
//          while ((len=bis.read(buff))!=-1) {
//              System.out.println(new String(buff,0 , len));
//          }

        // ② 读一个字节, 汉字为2个字节,读取汉字时出现乱码
        int b=0;
        StringBuilder sb=new StringBuilder();
        while ((b=bis.read())!=-1) {
            sb.append((char)b);

        }
        System.out.println(sb.toString());  
        bis.close();
    }

    /**
     * 读取的常见三种方式
     */
    private static void test() throws IOException {
        FileInputStream fis = fis=new FileInputStream("jar.txt");
        // ①每次读取一个字符数组,效率高
//          byte[] buff = new byte[1024];
//          int len=0;
//          while((len=fis.read(buff))!=-1){
//              System.out.print(new String(buff,0,len));
//          }

        // ② 每次读取一个字符 , 效率低
        int ch;
        while((ch=fis.read())!=-1){
            System.out.print((char)ch);
        }

        // ③ 创建的byte数组与流等大小,慎用,以防流过大,使创建字节数组失败
//          byte[] buf = new byte[fis.available()]; // 创建一个和流等大小的字节数组
//          fis.read(buf);
//          System.out.println(new String(buf));
            fis.close();
    }
}

2.13 ObjectInputStream

对象输入流,对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。

ObjectOutputStream 和 ObjectInputStream 分别与 FileOutputStream 和 FileInputStream 一起使用时,可以为应用程序提供对对象图形的持久存储。ObjectInputStream 用于恢复那些以前序列化的对象。其他用途包括使用套接字流在主机之间传递对象,或者用于编组和解组远程通信系统中的实参和形参。 只有支持 java.io.Serializable 或 java.io.Externalizable 接口的对象才能从流读取。

注意

①对象要能从流中读取,它必须是可序列化的
②读取的顺序应该与写入时的顺序是一致的          

2.13.1 构造方法与方法摘要

1.构造方法

  ObjectInputStream(InputStream in) 
          创建从指定 InputStream 读取的 ObjectInputStream。 

2.方法摘要
由于方法太多,不贴出具体摘要。除了从基类继承的方法,它主要功能是对基本类型与引用类型的读取。

 int readInt() 
          读取一个 32 位的 int 值。 
 Object readObject() 
          从 ObjectInputStream 读取对象。 
 String readUTF() 
          读取 UTF-8 修改版格式的 Stringint read() 
          读取数据字节。 
 int read(byte[] buf, int off, int len) 
          读入 byte 数组。 

2.13.2 使用示例

private static void read() throws FileNotFoundException, IOException,
            ClassNotFoundException {
        FileInputStream fis=new FileInputStream("t.tmp");
        ObjectInputStream ois=new ObjectInputStream(fis);
        // 顺序读取
        int readInt = ois.readInt();
        String string = (String) ois.readObject();
        Date date=(Date) ois.readObject();

        System.out.println("readInt="+readInt);
        System.out.println("string="+string);
        System.out.println("date="+date);

        ois.close();
    }

2.14 SequenceInputStream

序列流,用于将多个输入流串联起来,形成一个更大的流,以方便对多个文件的同时操作。
从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

2.14.1 构造方法与方法摘要

1.构造方法摘要

SequenceInputStream(Enumeration<? extends InputStream> e) 
              通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。 
SequenceInputStream(InputStream s1, InputStream s2) 
              通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节。 

2.方法摘要

 int available() 
          返回不受阻塞地从当前底层输入流读取(或跳过)的字节数的估计值,方法是通过下一次调用当前底层输入流的方法。 
 void close() 
          关闭此输入流并释放与此流关联的所有系统资源。 
 int read() 
          从此输入流中读取下一个数据字节。 
 int read(byte[] b, int off, int len) 
              将最多 len 个数据字节从此输入流读入 byte 数组。 

2.14.2 使用示例

1.两个流的情况
需求:把a.java和b.java的内容复制到copy.java中。
使用 SequenceInputStream(InputStream s1, InputStream s2)构造实现

public class SequenceInputStreamDemo {
    public static void main(String[] args) throws IOException {
        InputStream s1 = new FileInputStream("a.java");
        InputStream s2 = new FileInputStream("b.java");
        // SequenceInputStream(InputStream s1, InputStream s2)
        SequenceInputStream sis = new SequenceInputStream(s1, s2);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("copy.java"));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = sis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        sis.close();
    }
}

2.多个流的情况
需求:把三个文件a.java,b.java,c.java的内容复制到copy.java中
使用SequenceInputStream(Enumeration e)构造方法来实现多个流的串联

public class SequenceInputStreamDemo {
    public static void main(String[] args) throws IOException {
        // 创建Vector用于存储输入流
        Vector<InputStream> v = new Vector<InputStream>();
        InputStream s1 = new FileInputStream("a.java");
        InputStream s2 = new FileInputStream("b.java");
        InputStream s3 = new FileInputStream("c.java");
        v.add(s1);
        v.add(s2);
        v.add(s3);
        // Enumeration是Vector的elements()方法的返回值类型,Enumeration<E> elements()
        Enumeration<InputStream> en = v.elements();
        SequenceInputStream sis = new SequenceInputStream(en);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("copy.java"));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = sis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        sis.close();
    }
}

2.15 ByteArrayInputStream

ByteArrayInputStream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方法要提供的下一个字节。

关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。

2.15.1 构造方法

构造方法摘要 
ByteArrayInputStream(byte[] buf) 
          创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。 
ByteArrayInputStream(byte[] buf, int offset, int length) 
          创建 ByteArrayInputStream,使用 buf 作为其缓冲区数组。 

此类的方法与父类相同,侧重于读。不贴出了。

2.15.2使用示例

private static void test1() {
        byte[] buf=new byte[]{‘h‘,‘e‘,‘l‘,‘l‘,‘o‘};
        // 用一个字节数组来构造一个ByteArrayInputStream
        ByteArrayInputStream bais=new ByteArrayInputStream(buf,0,3);
        byte[] buff=new byte[bais.available()-1];
        try {
            bais.read(buff);
            System.out.println(new String(buff));
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                bais.close(); // 关闭 ByteArrayInputStream 无效,是一个空实现
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   

        // 仍然可以继续读
        int read = bais.read();
        System.out.println((char)read);
    }

2.16 PipedInputStream

管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。
通常,数据由某个线程从 PipedInputStream 对象读取,并由其他线程将其写入到相应的 PipedOutputStream。

不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。管道输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。如果向连接管道输出流提供数据字节的线程不再存在,则认为该管道已损坏。

管道连接的两种方式:

①在构造时以管道输出流对象做参数来构造,即使用PipedInputStream(PipedOutputStream src) 构造
②使用connect(PipedOutputStream src) 方法来建立连接

2.16.1 构造方法摘要与 方法摘要

构造方法摘要

PipedInputStream() 
          创建尚未连接的 PipedInputStream。 
PipedInputStream(int pipeSize) 
          创建一个尚未连接的 PipedInputStream,并对管道缓冲区使用指定的管道大小。 
PipedInputStream(PipedOutputStream src) 
          创建 PipedInputStream,使其连接到管道输出流 src。 
PipedInputStream(PipedOutputStream src, int pipeSize) 
          创建一个 PipedInputStream,使其连接到管道输出流 src,并对管道缓冲区使用指定的管道大小。 

方法摘要

 int available() 
          返回可以不受阻塞地从此输入流中读取的字节数。 
 void connect(PipedOutputStream src) 
          使此管道输入流连接到管道输出流 src。 
 int read() 
          读取此管道输入流中的下一个数据字节。 
 int read(byte[] b, int off, int len) 
          将最多 len 个数据字节从此管道输入流读入 byte 数组。 

2.16.2 使用示例

public class PipedStream {
    public static void main(String[] args) throws IOException {

        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream();

        // 使输入管道流与输出管道流联通
        input.connect(output);

        new Thread(new Input(input)).start();
        new Thread(new Output(output)).start();

    }

}

class Input implements Runnable {

    private PipedInputStream in;

    Input(PipedInputStream in) {
        this.in = in;
    }

    public void run() {

        try {
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = in.read(buf)) != -1) { //read 读取从管道输出流写入的数据,没有读到数据则阻塞
                String s = new String(buf, 0, len);
                System.out.println("s=" + s);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

class Output implements Runnable {
    private PipedOutputStream out;

    Output(PipedOutputStream out) {
        this.out = out;
    }

    public void run() {

        try {
            while (true) {  // 模拟数据写入
                Thread.sleep(3000);
                out.write("Output管道写入的数据!".getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.17 BufferedInputStream

为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。缓冲区用以减少频繁的IO操作,提高程序的性能。

在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。

构造方法摘要

BufferedInputStream(InputStream in) 
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 
BufferedInputStream(InputStream in, int size) 
              创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

此类的方法与父类无异,不贴出。 具体使用看下一节的性能测试。

2.18 BufferedInputStream性能测试

/**
 * @author pecu
 * 
 *         效率测试
 * 
 *         FileInputStream 
 *               ①read()    35950毫秒 
 *               ②read(buff) 62毫秒
 * 
 *         BufferedInputStream
 *               ③read()    295毫秒   
 *               ④read(buff) 18毫秒
 * 
 */
public class EfficiencyTest {

    public static void main(String[] args) throws IOException {
        //test1();
        //test2();
        //test3();
        test4();
    }

    // BufferedInputStream ④read(buff) 18毫秒
    private static void test4() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                "cpp.wmv"));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("cpp_copy4.wmv"));

        byte[] buff = new byte[1024];
        int len = 1;
        while ((len = bis.read(buff)) != -1) {
            bos.write(buff, 0, len);
        }

        bos.close();
        bis.close();
        System.out.println(System.currentTimeMillis() - start + "毫秒");
    }

    // BufferedInputStream ③read() 295毫秒
    private static void test3() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                "cpp.wmv"));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("cpp_copy3.wmv"));

        int b = 1;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }

        bos.close();
        bis.close();
        System.out.println(System.currentTimeMillis() - start + "毫秒");
    }

    // FileInputStream ②read(buff) 62毫秒
    private static void test2() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("cpp.wmv");
        FileOutputStream fos = new FileOutputStream("cpp_copy2.wmv");
        int len;
        byte[] buff = new byte[1024];
        while ((len = fis.read(buff)) != -1) {
            fos.write(buff, 0, len);
        }

        fis.close();
        fos.close();
        System.out.println(System.currentTimeMillis() - start + "毫秒");
    }

    // FileInputStream ①read() 35950毫秒
    private static void test1() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("cpp.wmv");
        FileOutputStream fos = new FileOutputStream("cpp_copy1.wmv");
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }

        fis.close();
        fos.close();
        System.out.println(System.currentTimeMillis() - start + "毫秒");
    }

}

2.2 OutputStream

此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。

需要定义 OutputStream 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。

方法摘要

注意:子类必须重写write方法

void close() 
          关闭此输出流并释放与此流有关的所有系统资源。 
 void flush() 
          刷新此输出流并强制写出所有缓冲的输出字节。 
 void write(byte[] b) 
          将 b.length 个字节从指定的 byte 数组写入此输出流。 
 void write(byte[] b, int off, int len) 
          将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 
abstract  void write(int b) 
          将指定的字节写入此输出流。

2.21 FileOutputStream

文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。

FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。

2.21.1 构造方法与方法摘要

1.构造方法摘要

FileOutputStream(File file) 
          创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 
FileOutputStream(File file, boolean append) 
          创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 
FileOutputStream(FileDescriptor fdObj) 
          创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。 
FileOutputStream(String name) 
          创建一个向具有指定名称的文件中写入数据的输出文件流。 
FileOutputStream(String name, boolean append) 
          创建一个向具有指定 name 的文件中写入数据的输出文件流。 

2.方法摘要
只列出了与父类相异的方法

 FileChannel getChannel() 
          返回与此文件输出流有关的唯一 FileChannel 对象。 
 FileDescriptor getFD() 
          返回与此流有关的文件描述符。 

2.21.2 使用示例

/**
* 从一个文件中读取,并将内容写入另一个文件
 * @throws IOException 
 */
private static void test() throws IOException {
        FileInputStream fis = fis = new FileInputStream("jar.txt");
        FileOutputStream fos = fos=new FileOutputStream("fos.txt",true); // true 表示可附加内容    
        byte[] buf=new byte[1024];
        int len=-1;
        while((len=fis.read(buf))!=-1){
            fos.write(buf, 0, len);
        }
        fis.close();
        fos.close();
}

2.22 ObjectOutputStream

将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。
通过在流中使用文件可以实现对象的持久存储。如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。

writeObject 方法用于将对象写入流中。所有对象(包括 String 和数组)都可以通过 writeObject 写入。
可将多个对象或基元写入流中。必须使用与写入对象时相同的类型和顺序从相应 ObjectInputstream 中读回对象。 

注意事项

①要写入流中的对应必须支持序列化,并且最好指定该对象的序列化UID
②反序列化的顺序与写入的顺序要一致
③使用transient关键字声明不需要序列化的成员变量

2.22.1 构造方法与方法摘要

1.构造方法摘要

 ObjectOutputStream(OutputStream out) 
          创建写入指定 OutputStream 的 ObjectOutputStream。 

2.方法摘要
方法较多,主要是写的操作,列出需要注意的几项

void writeBytes(String str) 
          以字节序列形式写入一个 Stringvoid writeObject(Object obj) 
          将指定的对象写入 ObjectOutputStream。  
void writeUTF(String str) 
          以 UTF-8 修改版格式写入此 String 的基本数据。 

2.22.2 使用示例

注意:要进行写入到流中的对象必须支持序列化

public class ObjectStreamDemo {
            public static void main(String[] args) throws IOException,
                    ClassNotFoundException {
                // 由于我们要对对象进行序列化,所以我们先自定义一个类
                // 序列化数据其实就是把对象写到文本文件
                // write();

                read();
            }

            private static void read() throws IOException, ClassNotFoundException {
                // 创建反序列化对象
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                        "oos.txt"));
                // 还原对象
                Object obj = ois.readObject();
                // 释放资源
                ois.close();
                // 输出对象
                System.out.println(obj);
            }

            private static void write() throws IOException {
                // 创建序列化流对象
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
                        "oos.txt"));
                // 创建对象
                Person p = new Person("林青霞", 27);
                // 写入对象
                oos.writeObject(p);
                // 释放资源
                oos.close();
            }
        }

2.23 ByteArrayOutputStream

此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。
可使用 toByteArray() 和 toString() 获取数据。

关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。

2.23.1 构造方法摘

1 构造方法摘要

ByteArrayOutputStream() 
          创建一个新的 byte 数组输出流。 
ByteArrayOutputStream(int size) 
          创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量(以字节为单位)。 

2 方法摘要
只列出了特有方法

 int size() 
              返回缓冲区的当前大小。 
 byte[] toByteArray() 
          创建一个新分配的 byte 数组。 
 String toString() 
          使用平台默认的字符集,通过解码字节将缓冲区内容转换为字符串。 
 String toString(String charsetName) 
          使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串。 
 void writeTo(OutputStream out) 
                  将此 byte 数组输出流的全部内容写入到指定的输出流参数中,这与使用 out.write(buf, 0, count) 调用该输出流的 write 方法效果一样。 

2.23.2 使用示例

private static void test() throws IOException {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte[] buf=new byte[]{‘h‘,‘e‘,‘l‘,‘l‘,‘o‘,‘w‘,‘o‘,‘r‘,‘l‘,‘d‘};
        baos.write(buf, 0, buf.length-5);
        System.out.println(baos.toString());
        baos.close(); // 空实现
        // 继续往流中写入
        baos.write(buf, 5, 5);
        System.out.println(baos.toString());
}

2.24 PipedOutputStream

可以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。

不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。如果某个线程正从连接的管道输入流中读取数据字节,但该线程不再处于活动状态,则该管道被视为处于 毁坏 状态。

构造方法摘要

PipedOutputStream() 
          创建尚未连接到管道输入流的管道输出流。 
PipedOutputStream(PipedInputStream snk) 
          创建连接到指定管道输入流的管道输出流。 

方法摘要

 void connect(PipedInputStream snk) 
          将此管道输出流连接到接收者。 
 void flush() 
          刷新此输出流并强制写出所有缓冲的输出字节。 
 void write(byte[] b, int off, int len) 
          将 len 字节从初始偏移量为 off 的指定 byte 数组写入该管道输出流。 
 void write(int b) 
          将指定 byte 写入传送的输出流。 

此流的使用可以看2.16小节的例子。

2.25 PrintStream

为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。它还提供其他两项功能。与其他输出流不同,PrintStream 永远不会抛出 IOException;而是,异常情况仅设置可通过 checkError 方法测试的内部标志。

可以创建一个自动刷新PrintStream;这意味着可在写入 byte 数组之后自动调用 flush 方法,可调用其中一个 println 方法,或写入一个换行符或字节 (‘\n’)来完成。

PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。

2.25.1 构造方法摘要

从构造方法可以看出,它既可以操作文件,也可以操作字节输出流

PrintStream(File file) 
          创建具有指定文件且不带自动行刷新的新打印流。 
PrintStream(File file, String csn) 
          创建具有指定文件名称和字符集且不带自动行刷新的新打印流。 
PrintStream(OutputStream out) 
          创建新的打印流。 
PrintStream(OutputStream out, boolean autoFlush) 
          创建新的打印流。 
PrintStream(OutputStream out, boolean autoFlush, String encoding) 
          创建新的打印流。 
PrintStream(String fileName) 
          创建具有指定文件名称且不带自动行刷新的新打印流。 
PrintStream(String fileName, String csn) 
          创建具有指定文件名称和字符集且不带自动行刷新的新打印流。 

方法就不贴了,大多都是打印的方法。

2.25.2 使用示例

我们常见的System.out返回值其实就是PrintStream

public class PrintStreamDemo{
    public static void main(String[] args) throws IOException {
        // 获取标准输入流
         PrintStream ps = System.out;
        // // OutputStream os = ps;
        // OutputStream os = System.out; // 多态
//       ps.println("hello");
//       ps.print(false);

        //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // 等价于下面两句
        // OutputStreamWriter osw = new OutputStreamWriter(os);
        // BufferedWriter bw = new BufferedWriter(osw);

        OutputStream out=new FileOutputStream("a.txt");
        PrintStream stream=new PrintStream(out, true); // 开启自动刷新
        stream.print(0);
        stream.print("hello");
        stream.println("world");

        stream.close();
        stream.append((char) 0); 
        stream.println("world"); // 关闭流打印,仍不会抛出异常。

    }
}

2.26 BufferedOutputStream

为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。缓冲区减少IO操作以提高性能。

在读取或跳过流中的字节时, 可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。

2.26.1构造方法摘要

BufferedInputStream(InputStream in) 
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 
BufferedInputStream(InputStream in, int size) 
          创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 

2.26.2使用示例

private static void test1() throws IOException {
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("bos.txt"));
        // 等价于下面两句
        //FileOutputStream fos=new FileOutputStream(new File("bos.txt"));
        //BufferedOutputStream bos=new BufferedOutputStream(fos);

        bos.write(66); // B的ASCII为66
        bos.write(new byte[]{‘h‘,‘e‘,‘l‘,‘l‘,‘0‘,‘~‘});
        bos.close(); 
        // 写入文件内容
        // Bhell0~
    }

至于它写的性能就不再测试了。

3 案例

复制一个多级文件夹到一个指定的目录下

public class CopyFolder {
            public static void main(String[] args) {
                String srcDir = "D:\\test\\666";
                String destDir = "D:\\test\\999";
                copyFolder(srcDir, destDir);
            }

            /**
             * 复制多级文件夹
             * 
             * @param srcDir
             *            要复制的文件目录,必须存在
             * @param destDir
             *            复制到那个目录下,可以不存在
             */
            public static void copyFolder(String srcDir, String destDir) {

                File dir = new File(srcDir);
                if (!dir.exists()) {
                    System.out.println("原目录" + dir.getAbsolutePath() + "不存在!");
                    return;
                }
                if (!dir.isDirectory()) {// 不是目录则返回
                    System.out.println(dir.getAbsolutePath() + "不是目录!");
                    return;
                }
                destDir += File.separator + dir.getName(); // 根据源目录构造目标目录
                File destFile = new File(destDir);
                if (!destFile.exists()) // 若目标目录不存在则创建之
                    destFile.mkdirs();
                File[] listFiles = dir.listFiles();
                if(listFiles == null) return;
                for (File file : listFiles) { // 遍历原文件夹
                    if (file.exists()) {
                        if (file.isDirectory()) { // 若是目录,继续遍历
                            copyFolder(file.getAbsolutePath(), destDir);
                        } else { // 复制文件到目的目录
                            CopyFile(file.getAbsolutePath(), destDir + File.separator
                                    + file.getName());
                        }
                    }
                }
            }

            /**
             * 复制文件
             * 
             * @param srcDir
             *            文件的源目录
             * @param destDir
             *            文件的目标目录
             */
            private static void CopyFile(String srcDir, String destDir) {
                File srcFile = new File(srcDir);
                File destFile = new File(destDir);
                if (!srcFile.exists()) // 原文件不存在则返回
                    return;
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream(srcFile)); // 关联文件输入流
                    bos = new BufferedOutputStream(new FileOutputStream(destFile)); // 关联文件输出流
                    int len = -1;
                    byte[] buff = new byte[2048];
                    while ((len = bis.read(buff)) != -1) { // 读
                        bos.write(buff, 0, len); // 写
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (bos != null) {
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

后记,代码都在一起,源码在下篇再贴上~ 欢迎交流学习~

Java之IO流---字节流

标签:

原文地址:http://blog.csdn.net/qq_28261343/article/details/52678681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!