IO流学习之文件字节流、文件字符流

2.1 文件字节流

FileInputStream通过字节的方式读取文件,适合读取所有类型的文件(图像、视频、文本文件等)。Java也提供了FileReader专门读取文本文件。

FileOutputStream 通过字节的方式写数据到文件中,适合所有类型的文件。Java也提供了FileWriter专门写入文本文件。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
    *将字符串/字节数组的内容写入到文件中
    * @author xzy
    * @date 2021/4/12 16:11
*/
public class TestFileOutputStream {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = null;
        String string = "努力去见你!";
        try{
            //true表示内容会追加到文件末尾;false表示重写整个文件
            fos = new FileOutputStream("F:/idea代码/IO流/a.txt",true);
            //该方法是直接将一个字节数组写入文件中;而write(int n)是写入一个字节
            fos.write(string.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            try{
                if(fos != null){
                    fos.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 利用文件流实现文件的复制
 * @author xzy
 * @date 2021/4/12 16:26
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileCopy {
    public static void main(String[] args) {
        //将a.txt内容拷贝到b.txt
        copyFile("F:/idea代码/IO流/a.txt", "F:/idea代码/IO流/b.txt");
    }

    /**
     * 将src文件的内容拷贝到dec文件
     * @param src 源文件
     * @param dec 目标文件
     */
    static void copyFile(String src, String dec) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        //为了提高效率,设置缓存数组!(读取的字节数据会暂存放到该字节数组中)
        byte[] buffer = new byte[1024];
        int temp = 0;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dec);
            //边读边写
            //temp指的是本次读取的真实长度,temp等于-1时表示读取结束
            while ((temp = fis.read(buffer)) != -1) {
                /*将缓存数组中的数据写入文件中,注意:写入的是读取的真实长度;
                 *如果使用fos.write(buffer)方法,那么写入的长度将会是1024,即缓存
                 *数组的长度*/
                fos.write(buffer, 0, temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //两个流需要分别关闭
            try{
                if(fos != null){
                    fos.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
            try{
                if(fis != null){
                    fis.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

 在使用文件字节流时,我们需要注意以下两点:

      1. 为了减少对硬盘的读写次数,提高效率,通常设置缓存数组。相应地,读取时使用的方法为:read(byte[] b);写入时的方法为:write(byte[ ] b, int off, int length)。

      2. 程序中如果遇到多个流,每个流都要单独关闭,防止其中一个流出现异常后导致其他流无法关闭的情况。

2.2文件字符流

前面介绍的文件字节流可以处理所有的文件,但是字节流不能很好的处理Unicode字符,经常会出现“乱码”现象。所以,我们处理文本文件,一般可以使用文件字符流,它以字符为单位进行操作

import java.io.*;

/**
    * 使用FileReader与FileWriter实现文本文件的复制
    * @author xzy
    * @date 2021/4/12 16:58
*/

public class TestFileCopy2 {
    public static void main(String[] args) {
        //写法和使用Stream基本一样,只不过,读取时读取的是字符
        FileReader fr = null;
        FileWriter fw = null;
        int len = 0;
        try {
            fr = new FileReader("F:/idea代码/IO流/a.txt");
            fw = new FileWriter("F:/idea代码/IO流/d.txt");
            //为了提高效率,设置缓存数组!(读取的字节数据会暂存放到该字节数组中)
            char[] buffer = new char[1024];
            //边读边写
            while ((len = fr.read(buffer)) != -1) {
                fw.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //两个流需要分别关闭
            try{
                if(fr != null){
                    fr.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
            try{
                if(fw != null){
                    fw.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值