CGBTN2110-DAY13总结复习

1 流的分类

1)按照方向分类:输入流 输出流
2)按照操作的单位分类:字节流 字符流
3)组合情况:字节输入流 字节输出流 字符输入流 字符输出流

2 字节输入流

1)抽象父级:InputStream–不能实例化
2)普通子级:

  1. FileInputStream–操作文件的字节输入流
    构造方法参数:File file / String pathname
  2. BufferedInputStream–高效字节输入流
    构造方法参数:InputStream,但无法创建抽象父级对象,所以传的是FileInputStream

3 字节输出流 OutputStream

1)抽象父级:OutputStream–不能实例化
2)普通子级:

  1. FileOutputStream–操作文件的字节输出流
    构造方法参数:File file / String pathname
    注意:默认存在一个参数boolean append,默认值为false,也就是覆盖输出
    如果将FileOutputStream构造函数的第2个参数appned设置为true,就会实现追加输出的效果
  2. BufferedOutputStream–高效字节输出流
    构造方法参数:OutputStream,但无法创建抽象父级对象,所以传的是FileOutputStream

4 字符输入流 Reader

1)抽象父级:Reader
2)普通子级:

  1. FileReader–操作文件的字符输入流
    构造方法参数:File file /String filename
  2. BufferedReader–高效字符输入流
    构造方法参数:Reader,但无法创建抽象父级对象,所以传的是FileReader

5 字符输出流 Writer

1)抽象父级:Writer
2)普通子级:

  1. FileWriter–操作文件的字符输出流
    构造方法参数:File file /String filename
    注意:默认存在一个参数boolean append,默认值为false,也就是覆盖输出
    如果将FileWriter构造函数的第2个参数appned设置为true,就会实现追加输出的效果
  2. BufferedWriter–高效字符输出流
    构造方法参数:Writer,但无法创建抽象父级对象,所以传的是FileWriter

6 文件复制综合案例

package cn.tedu.file;

import java.io.*;
import java.util.Scanner;

/*本类用于练习IO流文件复制综合案例*/
public class TestCopyFile {
    public static void main(String[] args) {
        //1.提示并接收用户输入的两个路径
        System.out.println("请输入源文件的路径:");
        String f = new Scanner(System.in).nextLine();//被复制的那个文件
        System.out.println("请输入新文件的路径:");
        String t = new Scanner(System.in).nextLine();//复制好的新文件
        //2.调用创建好的自定义方法,完成文件的复制
        //ZFCopy(f,t);//使用字符流完成复制操作
        ZJCopy(f,t);//使用字节流完成复制的操作
    }

    //使用字节流完成文件复制的操作
    private static void ZJCopy(String f, String t) {
        //1.定义两个在本方法中都生效的字节流局部变量,注意初始化值为null
        BufferedInputStream in = null;//高效字节输入流,用于读取
        BufferedOutputStream out = null;//高效字节输出流,用于写出
        //2.由于IO操作可能会发生异常,所以需要完成try-catch-finally结构
        try{
            //3.1创建一个高效字节输入流对象,用于读取源文件
            in = new BufferedInputStream(new FileInputStream(f));
            //3.2创建一个高效字节输出流对象,用于向新文件输出数据
            out = new BufferedOutputStream(new FileOutputStream(t));

            //4.使用两个流对象完成复制操作
            int b;
            while((b=in.read())!=-1){
                out.write(b);
            }
            System.out.println("恭喜!复制成功!");
        }catch (Exception e){
            System.out.println("复制失败!");
            e.printStackTrace();
        }finally {
            //5.关流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //使用字符流完成文件复制的操作
    private static void ZFCopy(String f, String t) {
        //1.定义两个在本方法中都生效的字符流局部变量,注意初始化值为null
        BufferedReader in = null;//高效字符输入流,用于读取
        BufferedWriter out = null;//高效字符输出流,用于写出
        //2.由于IO操作可能会发生异常,所以需要完成try-catch-finally结构
        try{
            //3.1创建高效字符输入流对象,用于读取源文件中的内容
            in = new BufferedReader(new FileReader(f));
            //3.2创建高效字符输出流对象,用于将读到的数据写出到新文件中
            out = new BufferedWriter(new FileWriter(t));

            //4.使用流对象完成复制的操作
            //4.1定义变量用来保存读到的数据
            int b;
            //4.2循环读取源文件,只要读到的数据不是-1,说明还有数据,继续读
            while((b = in.read())!=-1){
                //4.3 将本轮读到的数据,写出到新文件中,读一个,写一个
                out.write(b);
            }
            System.out.println("恭喜您!复制成功!");
        }catch (Exception e){
            System.out.println("很抱歉!复制失败!");
            e.printStackTrace();
        }finally {
            /*关流是有顺序的,如果有多个流,最后创建的流,最先关闭
            * 多条关流语句,需要各自try-catch*/
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

7 多种流创建方式复习

package cn.tedu.review;

import java.io.*;

/*本类用于复习常用流对象的创建方式*/
public class CreateStream {
    public static void main(String[] args) {
        f1();//用于复习字节流对象的创建
        f2();//用于复习字符流对象的创建
    }

    //复习字符流的创建方式
    private static void f2() {
        try {
            //用于复习字符输入流
            //Reader in = new Reader();//报错,抽象父级不可实例化
            //Reader in = new FileReader("");//可以,是多态的写法
            FileReader in1 = new FileReader(new File(""));
            FileReader in2 = new FileReader("");
            BufferedReader in3 = new BufferedReader(
                    new FileReader(new File("")));
            BufferedReader in4 = new BufferedReader(
                    new FileReader(""));

            //用于复习字符输出流
            //Writer out = new Writer();//报错,抽象父级不可实例化
            //Writer out = new FileWriter("");//可以,是多态的写法
            FileWriter out1 = new FileWriter(new File(""));//覆盖输出
            FileWriter out2 = new FileWriter("");//覆盖输出
            FileWriter out3 = new FileWriter(new File(""),true);//追加输出
            FileWriter out4 = new FileWriter("",true);//追加输出

            //高效字符输出流需要的参数是Writer,但是我们传入的是FW,以上4个流均可作为参数传入
            BufferedWriter out5 = new BufferedWriter(out4);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //复习字节流的创建方式
    private static void f1() {
        try {
            //用于复习字节输入流
            //InputStream in = new InputStream();//会报错,抽象父级不可实例化
            //InputStream in = new FileInputStream("");//这样写也可以,是多态的写法
            FileInputStream in = new FileInputStream(new File(""));
            FileInputStream in2 = new FileInputStream("");
            BufferedInputStream in3 = new BufferedInputStream(
                    new FileInputStream(new File("")));
            BufferedInputStream in4 = new BufferedInputStream(
                    new FileInputStream(""));

            //用于复习字节输出流
            //OutputStream out = new OutputStream();//报错,抽象父级无法实例化
            //OutputStream out = new FileOutputStream("");//可以,多态的写法
            FileOutputStream out1 = new FileOutputStream(new File(""));//覆盖输出
            FileOutputStream out2 = new FileOutputStream("");//覆盖输出
            FileOutputStream out3 = new FileOutputStream(new File(""), true);//追加输出
            FileOutputStream out4 = new FileOutputStream("", true);//追加输出

            //高效字节输出流需要的参数是OS,但是我们传入的是FOS,以上4个流均可作为参数传入
            BufferedOutputStream out5 = new BufferedOutputStream(out1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>