013字节流写出、字符流写出、综合练习

1.字节流写出

1.1OutPutStream抽象类

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

常用方法:
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)将指定的字节写入次输出流

1.2FileOutPutStream子类

直接插在文件上,直接写出文件数据

构造方法(创建对象)
FileOutPutStream(String Name)
创建一个具有指定名称的文件写入数据的文件输出流
FileOutStream(File file)
创建一个向指定File对象表示的文件导入写入数据的文件输出流
FileOutStream(File file,boolean append)如果第二个参数为true,表示追加,不覆盖创建一个向指定file对象表示的文件中写入数据的文件输出流,后面的参数是指是否覆盖源文件)

1.3 BufferedOutputStream子类

该类实现缓冲的输出流,通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必每次针对字节写出调用底层系统。

构造方法(创建对象):
BufferedOutPutStream(OutPutStream out)
创建一个新的缓冲输出流,用以将数据写入指定的底层输出流

1.4练习:字节输出流测试

package day13;

import java.io.*;

/**
 * 本类用于测试字节输出流
 */
public class Test1_OutPutStream {
    public static void main(String[] args) {
        method1();
        method2();
    }

    public static void method2() {
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream("D:\\5.txt"));
            out.write(121);
            out.write(122);
            out.write(123);
            out.write(124);
            out.write(125);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void method1()  {
        OutputStream out = null;
        try {
            out = new FileOutputStream(new File("D:\\4.txt"));
            out.write(100);
            out.write(101);
            out.write(102);
            out.write(103);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2 字符流输出

2.1Writer 抽象类

写入字符流的抽象类

常用方法
Abstract Void close()关闭此流,但要先刷新它
Void write(char []cbuf)写入字符数组
Void write(int c)写入单个字符
Void write (String str)写入字符串
Void write(String str,int off,int len)写入字符串的某一部分
Abstract void write(char []cbuf ,int off ,int len)写入字符数组的某一部分

2.2FileWriter 子类

用来写入字符文件的便捷类,此类的构造方法假定默认字符编码和默认缓冲区大小都是可以接受自己自定义这些值,可以先在FileOutPutStream上构造一个OutPutStreamWriter.

构造方法(创建对象)
FileWriter(String filename)
根据给定的文件名构造一个File Writer对象
FileWriter(String filename,boolean append)根据给定的文件以及指示是否附加写入数据的boolean值来构造FileWriter

2.3BufferedWriter子类

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入,可以指定缓冲区的大小,或者接受默认的大小,在大多数情况下,默认值就足够大了。

构造方法
BufferedWriter(Write out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流

2.4练习:字符输出流测试

package day13;

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

/*
本类用于测试IO输入输出综合练习案例
 */
public class Test3_CopyFile {
    public static void main(String[] args) {
        System.out.println("请输入原文家路径");
        String f = new Scanner(System.in).nextLine();
        System.out.println("请输入目标文件路径");
        String t = new Scanner(System.in).nextLine();
        File from = new File(f);
        File to = new File(t);
        ZFCopy(from, to);
        ZJCopy(from, to);
    }

    private static void ZJCopy(File from, File to) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(from));
            outputStream = new BufferedOutputStream(new FileOutputStream(to));
            int b = 0;
            byte[] bs = new byte[8 * 1024];
            while (-1 != (b = inputStream.read(bs))) {
                outputStream.write(bs);
            }
            System.out.println("恭喜您复制成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("很抱歉,复制失败");
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void ZFCopy(File from, File to) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(from));
            out = new BufferedOutputStream(new FileOutputStream(to));
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            System.out.println("恭喜您复制成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

拓展

IO的继承结构

  1. 主流分类
    1.1 按照方向进行分类:输入流、输出流(相对于程序而言,从程序写数据到文件中是输出)
    1.2 按照传输类型进行分类:字节流、字符流
    1.3 组合:字节输入流、字符输入流、字节输出流、字符输出流
  2. 学习方法:在抽象父类中学习通用的方法、在子类中学习如何创建对象
  3. 字节输入流:
    —InputStream 抽象类,不能new,可以作为超类,学习其提供的共性方法
    —FileInputStream子类,操作文件的字节输入流,普通类
    —BufferedInputStream子类,缓冲字节输入流,普通类
  4. 字符输入流
    —Reader抽象类,不能new,可以作为超类,学习其提供的共性方法
    —FileReader,子类,操作文件的字符输入流,普通类
    —BufferedFileReader,子类,缓冲字符输入流,普通类
  5. 字节输出流
    —OutputStream抽象类,不能new,可以作为超类,学习其提供的共性方法
    —FileOutPutStream,子类,作为文件的字节输出流,普通类
    —BufferedFileoutputStream,子类,缓冲字节输出流,普通类
  6. 字符输出流
    —Writer抽象类,不能new,可作为超类,学习其所提供的共性方法
    —FileWriter,子类,可以操作文件的字符输出流
    —BufferedFilewriter,子类,缓冲字符输出流,普通类
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值