Java中字节流、字符流、转换流

字节流:

特点:能读写文本、图片、视频、音频等
主要字节流:
FileOutputStream:文件输出流
FileInputStream:文件输入流
案例:
需求:复制文件
public static void fun() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream("/Users/lanou/Desktop/Test/znb.txt");
        fos = new FileOutputStream("/Users/lanou/Desktop/Test/haha.txt");
        int len = 0;
        byte[] b = new byte[1024];
        while((len = fis.read(b)) != -1 ) {
            fos.write(b, 0, len);
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("文件找不到");
    } catch (IOException e) {
        throw new RuntimeException("文件复制失败");
    } finally {
        try {
            if(fis = null){
                fis.close();
            }
        } catch (IOException e) {
            throw new RuntimeException("资源关闭失败");
        } finally {
            try {
                if(fos = null){
                    fos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException("资源关闭失败");
            }
        }
    }
}
需求:复制文件夹
/*
 * 复制文件夹的方法
 */
public static void copyDir(File src, File dest) throws IOException {
    File newDir = new File(dest, src.getName());
    newDir.mkdir();
    File[] listFiles = src.listFile();
    for(File subFile : listFiles) {
        if (subFile.isFile()) {
            FileInputStream fis = new FileInputStream(subFile);
            File tempFile = new File(newDir, subFile.getName());
            FileOutputStream fos = new FileOutputStream(tempFile);
            int len = 0;
            byte[] b = new byte[1024];
            while((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
            fis.close();
            fos.close();
        } else {
            copyDir(subFile, newDir);
        }
    }
}
/*
 * 测试
 */
public static void main(String[] args) throws IOException {
    File src = new File("/Users/lanou/Desktop/Test");
    File dest = new File("/Users/lanou/Desktop/XTest");
    copyDir(src, dest);
}
需求:将指定文件下所有txt文件复制到另一个文件夹下
/*
 * 过滤器:过滤到后缀不是txt的文件
 */
class MyFilerByTxt implements FileFiler {
    public boolean accept(File pathname) {
        if (pathname.isDirectory()) {
            return ture;
        }
        return pathname.getName().endsWith("txt");
    }
}
/*
 * 复制文件的方法
 */
public static void copyDirTxtFile(File src, File dest) throws IOException {
    File[] listFiles = src.listFile(new MyFilerByTxt()) 
    for (File subFile : listFiles) {
        if (subFile.isFile()) {
            FileInputStream fis = new FileInputStream(subFile);
            File tempFile = new File(dest, subFile.getName());
            FileOutputStream fos = new FileOutputStream(tempFile);
            int len = 0;
            byte[] b = new byte[1024];
            while((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
            fis.close();
            fos.close();
        } else {
           copyDirTxtFile(subFile, dest);
        }
    }
}
/*
 * 测试
 */
public static void main(String[] args) throws IOException {
    File src = new File("/Users/lanou/Desktop/Test");
    File dest = new File("/Users/lanou/Desktop/XTest");
    copyDirTxtFile(src, dest);
}

字符流:


字符输出流(Writer):
是抽象类,是所有字符输出流的父类
注意:只能对文本文件进行操作
mac系统默认使用UTF-8的编码表(通用编码表),一个字符占3个字节。
Windows系统默认使用的GBK的编码表(简体中文),一个字符占2字节。
主要字符输出流:FileWriter
注意:字符输出流,写入文件时,要调用刷新方法。
字符输入流(Reader):
是抽象类,是所有字符输出流的父类
主要字符输入流:FileReader
注意:字符输入时, 不能字符串输入。 
      因为字符串很难界定,难以判断

转换流:


转换流的运行过程:

这里写图片描述

结论:
OutputStreamWriter(字符流转向字节流):
作用:根据不同编码格式写入
需要使用FileOutputStream类
InputStreamReader(字节流转向字符流):
作用:读取不同编码格式的文件
需要使用FileInputStream类
案例:
/*
 * UTF8写
 */
public static void getUTF8() throws IOException {
    FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/utf8.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write("张三");
    osw.close();
}

/*
 * UTF8读
 */
public static void readerUTF8() throws IOException {
    FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/utf8.txt");
    InputStreamReader isr = new InputStreamReader(fis);
    int len = 0;
    char[] c = new char[1024];
    while ((len = isr.read(c)) != -1) {
        System.out.println(new String(c, 0, len));
    }
    isr.close();
}
/*
 * GBK写
 */
public static void getGBK() throws IOException {
    FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/gbk.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
    osw.write("张三");
    osw.close();
}

/*
 * GBK读
 */
public static void readerGBK() throws IOException {
    FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/gbk.txt");
    InputStreamReader isr = new InputStreamReader(fis, "GBK");
    int len = 0;
    char[] c = new char[1024];
    while ((len = isr.read(c)) != -1) {
        System.out.println(new String(c, 0, len));
    }
    isr.close();
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值