IO流

IO流

什么是IO流

一个流可以理解为一个数据的序列,IO流(输入输出流)就是一种用来处理设备之间数据传输的流,Java中对数据的操作就是通过流来实现的。

IO流的分类

  • 按照数据流向分:输入流和输出流

  • 按照读写文件类型分:
    字节流(读写任意类型的文件)
    字符流(读写文本文件)

字节流

  • 字节流的基类:
InputStream 输入流  OutPutStream 输出流
  • 继承关系:在这里插入图片描述
字节输出流
构造方法功能
FileOutputStream(File file)创建一个向指定File文件中写入数据的文件输出流
FileOutputStream(String name)创建一个向具有指定名称的文件中写入数据的文件输出流
File file = new File("a.txt");
//输入流,所关联的文件,如果该文件不存在 ,那么会自动帮你创建
FileOutputStream out = new FileOutputStream(file);

FileOutputStream fileOutputStream = new FileOutputStream("b.txt");
  • 注意:流用完之后必须释放资源
out.close();
fileOutputStream.close();
字节输入流
构造方法功能
FileInputStream(String name)通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定
FileInputStream(File file)通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定
File file = new File("e.txt");
//输入流所关联的文件,如果不存在,就会报错
FileInputStream in = new FileInputStream(file);

FileInputStream in2 = new FileInputStream("e.txt");

out.close();
fileOutputStream.close();
高效字节流
构造方法功能
BufferedInputStream(InputStream in)创建一个BufferedInputStream并保存其参数
BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流

字节流复制文件

  1. 通过字节流一次读写一个字符:
public class 字节流复制 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream(new File("d:\\\\video01.avi"));
        FileOutputStream out = new FileOutputStream(new File("d:\\\\video02.avi"));
        int len=0;
        while ((len=in.read())!=-1){
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }
}
  1. 通过字节流一次读写一个字符数组:
public class 字节流复制 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream(new File("d:\\\\video01.avi"));
        FileOutputStream out = new FileOutputStream(new File("d:\\\\video02.avi"));
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}
  1. 通过高效字节流:
public class 一次读写一个字符 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\\\video01.avi")));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:\\\\video02.avi")));
        int len=0;
        while ((len=in.read())!=-1){
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }
}
public class 一次读写一个字符数组 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\\\video01.avi")));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:\\\\video02.avi")));
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

字符流

  • 字符流的基类:
Reader 输入流   Writer 输出流
  • 继承关系:
    在这里插入图片描述

字符转换流

OutputStreamWriter: 是字符流通向字节流的桥梁,可使用指定的charset将要写入流中的字符编码成字节。

InputStreamReader :是字节流通向字符流的桥梁:它使用指定的charset 读取字节并将其解码为字符。

构造方法功能
InputStreamReader(InputStream in)创建一个使用默认字符集的 InputStreamReader
OutputStreamWriter(OutputStream out)创建使用默认字符编码的 OutputStreamWriter
  • 使用字符流复制文本文件
public class 字符流复制 {
    public static void main(String[] args) throws IOException {
        InputStreamReader in = new InputStreamReader(new FileInputStream("D:\\IO流.txt"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("E:\\IO流.txt"));
        int len=0;
        char[] chars = new char[1024];
        while ((len=in.read(chars))!=-1){
            out.write(chars,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

高效字符流

  • 构造方法:
构造方法功能
BufferedWriter(Writer out)创建一个使用默认大小输出缓冲区的缓冲字符输出流
BufferedReader(Reader in)创建一个使用默认大小输入缓冲区的缓冲字符输入流
  • 高效字符流特有方法:
特有方法功能
void newLine()写入一个行换行符
String readLine()一次读取一行数据
  • 使用高效字符流复制文本文件
public class 高效字符流复制 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("Homy.java"));
        BufferedWriter writer = new BufferedWriter(new FileWriter("Homy1.java"));
        String line=null;
        while ((line=reader.readLine())!=null){
            writer.write(line);
            writer.newLine();
            writer.flush();
        }
        reader.close();
        writer.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值