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

Java中字节流和异常处理

时间:2016-07-14 10:27:37      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

字节输入流

字节输入流流程

package com.cloud.day2;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

/*

 File: 用于描述一个文件或者文件夹的。

 通过File对象我们可以读取文件或者文件夹的属性数据,如果我们需要读取文件的内容数据,那么我们需要使用IO流技术。

IO流(Input Output

IO流解决问题:解决设备与设备之间的数据传输问题。  内存--->硬盘   硬盘--->内存

IO流技术:

IO流分类:

   如果是按照数据的流向划分:

      输入流


      输出流


   如果按照处理的单位划分:


      字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。

      字符流:字符流读取的数据是以字符为单位的字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能识别的字符。 

           字符流 = 字节流 + 解码

         

输入字节流:

--------| InputStream 所有输入字节流的基类  抽象类

------------| FileInputStream  读取文件数据的输入字节流

      

使用FileInputStream读取文件数据的步骤:

   1. 找到目标文件

   2. 建立数据的输入通道。

   3. 读取文件中的数据。

   4. 关闭资源.

 */

public class Demo1 {

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

      readTest4();

   }

   //读取的方式一缺陷:无法读取完整一个文件的数据.

   public static void readTest1() throws IOException{

      //1.找到目标文件

      File file = new File("F:\\a.txt");

      //2.建立数据通道

      FileInputStream fileInputStream = new FileInputStream(file);

      //3.读取文件的数据

      int content = fileInputStream.read();

      System.out.println("read content:"+(char)content);

      //4.关闭释放资源

      fileInputStream.close();

   }


   //方式2 使用循环读取文件的数据

   public static void readTest2() throws IOException{

      long startTime = System.currentTimeMillis();

      File file = new File("F:\\dd.jpg");

      FileInputStream fileInputStream = new FileInputStream(file);

      int content = 0;

      while((content = fileInputStream.read())!=-1){

        System.out.print((char)content);

      }

      fileInputStream.close();

      long endTime = System.currentTimeMillis();

      System.out.println("读取时间:"+(endTime-startTime));

   }

  

   //方式3:使用缓冲数组读取。    缺点:无法读取完整一个文件的数据。

   public static void readTest3() throws IOException{

      File file = new File("F:\\a.txt");

      FileInputStream fileInputStream = new FileInputStream(file);

      byte []buf = new byte[1024];

      // 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,

      //而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。

      int len = fileInputStream.read(buf);

      System.out.println("len="+len);

      //使用字节数组构建字符串

      String content = new String(buf, 0, len);

      System.out.println("content:"+content);

      fileInputStream.close();

   }

  

   //方式4:使用缓冲数组配合循环一起读取

   public static void readTest4() throws IOException{

      long startTime = System.currentTimeMillis();

      File file = new File("F:\\dd.jpg");

      FileInputStream fileInputStream = new FileInputStream(file);

      int len = 0;

      //存储读取到的数据    缓冲数组的长度一般是1024的倍数,因为与计算机的处理单位。 

      //理论上缓冲数组越大,效率越高

      byte [] buf = new byte[1024];

      while((len = fileInputStream.read())!=-1){

        System.out.println(new String(buf,0,len));

      }

      fileInputStream.close();

      long endTime = System.currentTimeMillis();

      System.out.println("读取时间:"+(endTime-startTime));

   }

}

流关闭问题

先打开的流后关闭

package com.cloud.day2;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

/*

 问题1读取完一个文件的数据的时候,我不关闭资源有什么影响?

 答案:资源文件一旦使用完毕应该马上释放,否则其他的程序无法对该资源文件进行其他的操作。

 */

public class Demo2 {

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

      //1.找到目标文件

      File file = new File("F:\\a.txt");

      //2.建立数据输入通道

      FileInputStream fileInputStream = new FileInputStream(file);

      //3.缓冲字节数组读取文件

      byte []buf = new byte[1024];

      int length = 0;

      while((length=fileInputStream.read(buf))!=-1){

        System.out.println(new String(buf));

      }

      //4.释放资源文件

      fileInputStream.close();

   }

}

拷贝图片

package com.cloud.day3;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/*

需求:拷贝一张图片

 */

public class Demo1 {

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

      File inFile = new File("F:\\dd.jpg");

      File destFile = new File("E:\\dd.jpg");

      FileInputStream fileInputStream = new FileInputStream(inFile);

      //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。

      //每写出一次,指向都会出现相应移动。

      FileOutputStream fileOutputStream = new FileOutputStream(destFile);

      //建立缓冲数据,边读边写

      byte []buf = new byte[1024];

      int length=0;

      while((length=fileInputStream.read())!=-1){

        fileOutputStream.write(buf, 0, length);

      }

      //关闭原则,先开后关,后开先关

      fileOutputStream.close();

      fileInputStream.close();

   }

}

字节输出流

字节输出流流程

package com.cloud.day3;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;


/*

 字节输出流:


---| OutputStream 是所有输出字节流的父类。  抽象类

------| FileOutStream 向文件输出数据的输出字节流。

 

FileOutputStream如何使用呢?

   1. 找到目标文件

   2. 建立数据的输出通道。

   3. 把数据转换成字节数组写出。

   4. 关闭资源

 

FileOutputStream要注意的细节:

   1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。

   2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。

   3. 使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true

   4. 使用FileOutputStreamwrite方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是

         把低八位的二进制数据写出,其他二十四位数据全部丢弃。

 */

public class Demo2 {

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

      writeTest1();

   }

   //使用字节数组写出数据

  

   //每次只能写一个字节的数据出去

   public static void writeTest1() throws IOException{

      File file = new File("F:\\a.txt");

      FileOutputStream fileOutputStream = new FileOutputStream(file);

      fileOutputStream.write(‘s‘);

      fileOutputStream.write(‘p‘);

      fileOutputStream.write(‘r‘);

      fileOutputStream.write(‘i‘);

      fileOutputStream.write(‘n‘);

      fileOutputStream.write(‘g‘);

      fileOutputStream.close();

   }

}

写数据的类型

package com.cloud.day3;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Arrays;

/*

4.使用FileOutputStreamwrite方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一

个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。

 */

public class Demo3 {

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

      readTest();

   }

   public static void readTest() throws IOException{

      File file = new File("F:\\a.txt");

      FileInputStream fileInputStream = new FileInputStream(file);

      byte []buf=new byte[1024];

      int length = fileInputStream.read(buf);

      System.out.println("字节数组的内容是:"+Arrays.toString(buf));

      fileInputStream.close();

   }

   public static void writeTest()throws FileNotFoundException,IOException{

      File file = new File("F:\\a.txt");

      FileOutputStream fileOutputStream = new FileOutputStream(file);

      fileOutputStream.write(511);

      fileOutputStream.close();

   }

}

IO流的异常处理

package com.cloud.day3;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/*

IO中的异常处理

 */

public class Demo4 {

   public static void main(String[] args) {

      copyImage();

   }

   //拷贝图片

   public static void copyImage(){

      FileInputStream fileInputStream = null;

      FileOutputStream fileOutputStream = null;

      try {

        //找到目标文件

        File inFile = new File("F:\\dd.jpg");

        File outFile = new File("F:\\d.jpg");

        //建立流通道

        fileInputStream = new FileInputStream(inFile);

        fileOutputStream = new FileOutputStream(outFile);

        //建立缓冲数组,边读边写

        byte[] buf = new byte[1024];

        int length = 0;

        while((length = fileInputStream.read(buf))!=-1){

           fileOutputStream.write(buf, 0, length);

        }

      } catch (IOException e) {

        System.out.println("拷贝图片出错...");

        throw new RuntimeException(e);

      }finally{

        try {

           if(fileOutputStream!=null){

              fileOutputStream.close();

              System.out.println("关闭输出流对象成功...");

           }

        } catch (IOException e) {

           System.out.println("关闭输出流对象失败...");

           throw new RuntimeException(e);

        }finally{

           if(fileInputStream!=null){

              try {

                 fileInputStream.close();

                 System.out.println("关闭输入流成功...");

              } catch (IOException e) {

                 System.out.println("关闭输入流失败...");

                 throw new RuntimeException(e);

              }

           }

        }

      }

   }

  

   //读取文件测试

   public static void readTest(){

      FileInputStream fileInputStream = null;

      try {

        //找到目标文件

        File file = new File("F:\\a.txt");

        //建立数据输入通道

        fileInputStream = new FileInputStream(file);

        //建立缓冲数组读取数据

        byte[] buf = new byte[1024];

        int length = 0;

        while((length = fileInputStream.read(buf))!=-1){

           System.out.println(new String(buf, 0, length));

        }

      } catch (IOException e) {

        /*

         * 异常:阻止后面的代码执行,通知调用者这里出错

         * RuntimeException(e)

         * IOException传递给RuntimeException包装一层,然后抛出,使调用者使用变的灵活

         */

        System.out.println("读取资源文件出错...");

        throw new RuntimeException(e);

      }finally{

        try {

           if(fileInputStream!=null){

              fileInputStream.close();

              System.out.println("关闭资源文件成功...");

           }

        } catch (IOException e) {

           System.out.println("关闭资源文件失败...");

           throw new RuntimeException(e);

        }

      }

   }

}

缓冲字节流

缓冲字节输入流

package com.cloud.day3;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/*

 读取文件数据使用缓冲流效率最高--缓冲输入字节流对象


输入字节流体系

--|InputStream 输入字节流基类,抽象

----|FileInputStream 读取文件数据的输入字节流

----|BufferedInputStream 缓冲输入字节流  提高读取文件数据的效率,该类的内部维护了一个8kb的字节数组


注意:缓冲流不具备读写文件的能力

 */

public class Demo5 {

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

      readTest();

   }

   public static void readTest2() throws IOException{

      File file = new File("F:\\a.txt");

      FileInputStream fileInputStream = new FileInputStream(file);

      BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

      bufferedInputStream.read();

     

      FileOutputStream fileOutputStream = new FileOutputStream(file);

      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

      fileOutputStream.write(null);


      //读取文件数据

      int content = 0;

      while((content = fileInputStream.read())!=-1){

        System.out.println((char)content);

      }

      //关闭资源,这里实际上是关闭FileInputSream

      bufferedInputStream.close();

   }

   public static void readTest() throws IOException{

      //读取文件使用缓冲数组

      File file = new File("F:\\a.txt");

      FileInputStream fileInputStream = new FileInputStream(file);

      //建立缓冲数组读取数据

      byte[] buf = new byte[1024];

      int length = 0;

      while((length = fileInputStream.read(buf))!=-1){

        System.out.println(new String(buf, 0, length));

      }

      fileInputStream.close();

   }

}

缓冲字节输出流

package com.cloud.day3;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

/*

输出字节流

--|OutputStream 输出字节流的基类

----|FileOutputStream 输出字节流

----|BufferedOutputStream 缓冲字节输出流


注意:

   1.BufferedOutputStream写数据的时候,它的write方法是先把数据写到它维护的字节数组中

   2.BufferedOutputStream写数据的时候,它的write方法是先把数据写到它维护的字节数组中,如果需要把数据真正的

      写到硬盘上面,调用flush方法或者是close方法,或者是内部维护的字节数组已经填满的时候

 */

public class Demo6 {

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

      File file = new File("F:\\a.txt");

      FileOutputStream fileOutputStream = new FileOutputStream(file);

      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

      bufferedOutputStream.write("Hello,world".getBytes());

      bufferedOutputStream.close();

   }

}

缓冲字节流拷贝图片

package com.cloud.day3;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/*

拷贝图片

 */

public class Demo7 {

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

      //找到目标文件

      File inFile = new File("F:\\dd.jpg");

      File outFile = new File("F:\\1.jpg");

      //建立数据通道

      FileInputStream fileInputStream = new FileInputStream(inFile);

      FileOutputStream fileOutputStream = new FileOutputStream(outFile);

      //建立缓冲流

      BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

      //边读边写

      int content = 0;

      while((content = bufferedInputStream.read())!=-1){

        bufferedOutputStream.write(content);

      }

      bufferedInputStream.close();

      bufferedOutputStream.close();

   }

}

Java中字节流和异常处理

标签:

原文地址:http://blog.csdn.net/dzy21/article/details/51905723

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