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

java—— 字节流

时间:2018-02-26 23:24:41      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:exception   throw   创建   AC   数据类型   inpu   基本   inf   文件复制   

  字节流的继承图

  技术分享图片

  主要的字节流有:

  InputStream/OutputStream: 这是基类,它们是抽象类。

    FileInputStream/FileOutputStream: 输入源和输出目标是文件的流。

       ByteArrayInputStream/ByteArrayOutputStream: 输入源和输出目标是字节数组的流。

  DataInputStream/DataOutputStream: 装饰类,按基本类型和字符串而非只是字节读写流。

  BufferedInputStream/BufferedOutputStream: 装饰类,对输入输出流提供缓冲功能。

一、InputStream/OutputStream

  技术分享图片

 

     read从流中读取下一个字节,返回类型为int,但取值在0到255之间,当读到流结尾的时候,返回值为-1

 

 

  技术分享图片

二、FileInputStream/FileOutputStream 文件流  

  FileOutputStream的主要构造方法有

    public FileOutputStream(File file) throws FileNotFoundException

    public FileOutputStream(File file, boolean append) throws FileNotFoundException

    public FileOutputStream(String name) throws FileNotFoundException

    public FileOutputStream(String name, boolean append) throws FileNotFoundException

  从构造方法上可得出:可以是File对象file,也可以是文件路径name。

  备注:append参数指定是追加还是覆盖,true表示追加,默认为 false。

  FileInputStream的主要构造方法有

    public FileInputStream(String name) throws FileNotFoundException

    public FileInputStream(File file) throws FileNotFoundException

  演示代码实现文件复制

 1 public static void main(String[] args) throws IOException {
 2         InputStream in = new FileInputStream("D:\\mys\\2.jpg");
 3         File copy = new File("D:\\mys\\2copy.jpg");
 4         OutputStream out = new FileOutputStream(copy);
 5         byte[] b = new byte[10];
 6         int len = 0;
 7         while((len = in.read(b)) != -1){
 8              out.write(b, 0, len);
 9         }
10         in.close();
11         out.close();
12     }

  创建文件流对象会实际打开文件,然后操作系统会分配相关资源。如果当前用户没有写权限,会抛出异常SecurityException(父类  RuntimeException)。如果指定的文件是一个已存在的目录,或者由于其他原因不能打开文件,会抛出异常FileNotFoundException,它是IOException的一个子类。

三、ByteArrayInputStream/ByteArrayOutputStream    字节数组流

  ByteArrayOutputStream的输出目标是一个byte数组,这个数组的长度是根据数据内容动态扩展的。它有两个构造方法:

    public ByteArrayOutputStream()

    public ByteArrayOutputStream(int size)

    size指定的就是初始的数组大小(默认 32)

  ByteArrayOutputStream中的数据也可以方便的写到另一个OutputStream:

    public synchronized void writeTo(OutputStream out) throws IOException

  ByteArrayInputStream将byte数组包装为一个输入流,是一种适配器模式,它的构造方法有:

    public ByteArrayInputStream(byte buf[])

    public ByteArrayInputStream(byte buf[], int offset, int length)

    以buf中offset开始可以添加的 length 个字节数据

四、DataInputStream/DataOutputStream    数据操作流

  允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型

  DataOutputStream的构造方法:

    public DataOutputStream(OutputStream out)

  技术分享图片

  DataInputStream的构造方法:

    public DataInputStream(InputStream in)

  技术分享图片

  写的顺序与读的顺序要保持一致

  备注:DataInputStream是装饰类基类FilterInputStream的子类,FilterInputStream是InputStream的子类。

 五、BufferedInputStream/BufferedOutputStream   缓冲流

   缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法

 

     BufferedInputStream 内部有个字节数组作为缓冲区,读取时,先从这个缓冲区读,缓冲区读完了再调用包装的流读,它的构造方法有两个:

      public BufferedInputStream(InputStream in)

      public BufferedInputStream(InputStream in, int size)

         size表示缓冲区大小,默认值为8192。

   BufferedOutStream 内部有个字节数组作为缓冲区,它的构造方法也有两个:

      public BufferedOutStream(InputStream in)

      public BufferedOutStream(InputStream in, int size)

        size表示缓冲区大小,默认值为8192。

  使用输出流缓冲输入流实现文件复制

 1 public static void main(String[] args) throws IOException {
 2         InputStream in = new FileInputStream("D:\\mys\\2.jpg");
 3         File copy = new File("D:\\mys\\2copy.jpg");
 4         OutputStream out = new FileOutputStream(copy);
 5         BufferedInputStream bfi = new BufferedInputStream(in);
 6         byte[] b = new byte[10];
 7         int len = 0;
 8         while ((len = bfi.read(b)) != -1) {
 9             out.write(b, 0, len);
10         }
11         out.close();
12         //关闭缓冲流时,会自动关闭它所包装的底层节点流
13         bfi.close();
14     }

  向文件中写出数据

1 public static void main(String[] args) throws IOException {
2          FileOutputStream fos=new FileOutputStream("BOSDemo.txt");
3          BufferedOutputStream bos=new BufferedOutputStream(fos);
4          String content="我是缓冲输出流测试数据!";
5          bos.write(content.getBytes(),0,content.getBytes().length);
6          //输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
7          bos.flush();
8          bos.close();
9     }

专门的类库如Apache Commons IO

 

java—— 字节流

标签:exception   throw   创建   AC   数据类型   inpu   基本   inf   文件复制   

原文地址:https://www.cnblogs.com/SacredOdysseyHD/p/8476022.html

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