天天看点

Java中字节流和异常处理字节输入流字节输出流IO流的异常处理缓冲字节流

字节输入流

字节输入流流程

package com.cloud.day2;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

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;

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;

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;

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;

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) {

        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;

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;

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();

   }

}