节点流

节点流

/**
 * @description NodeIOTest
 * @author 小邱
 * @version 0.0.1
 * @since 2021/9/9 13:36
 */

import org.junit.Test;

import java.io.*;

//节点流(文件流)
public class NodeIOTest {
    @Test
    //FileReader
    public void test1() {
        //1、实例化File类对象,指明要操作的文件
        File file = new File("F:\\Lean\\Text\\src\\main\\resources\\a.txt");

        //2、提供FileReader流
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            //3、数据读入
            //一次读一个字符
//            int read1 = fr.read();
//            while (read1 != -1){
//                System.out.print((char)read1);
//                read1 = fr.read();
//            }

            //每次读取指定个数的字符
            char[] chars = new char[5];
            int len;
            while ((len = fr.read(chars)) != -1) {
                //错误的遍历
//                for (char aChar : chars) {
//                    System.out.print(aChar + "\t");
//                }
                //正确的遍历
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i] + "\t");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭流
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    //FileWriter
    public void test2() {
        //1、实例化File类对象,指明要操作的文件
        File file = new File("F:\\Lean\\Text\\src\\main\\resources\\a.txt");

        //2、提供FileWriter流
        FileWriter fw = null;
        try {
            /*
            file文件不存在会自动创建并写入数据
            若file文件存在则根据FileWriter()构造器参数指定是否覆盖或追加
            默认/false-->覆盖,true-->追加
             */
            fw = new FileWriter(file, true);
            //3、数据写出
            fw.write("hello\n"); /* \n表示换行 */
            fw.write("java");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭流
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    //FileReader与FileWriter实现文本文件复制
    public void test3() {
        //1、创建File类的对象,指明读入和写出的文件
        File srcFile = new File("F:\\Lean\\Text\\src\\main\\resources\\a.txt");
        File destFile = new File("F:\\Lean\\Text\\src\\main\\resources\\b.txt");

        //2、创建输入流和输出流
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);

            //3、数据的读入和写出
            char[] chars = new char[5];
            int len;//记录每次读取到数组的字符个数
            while ((len = fr.read(chars)) != -1) {
                //每次写出len个字符
                fw.write(chars, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭流
            try {
                if (fr != null) {
                    fr.close();
                }
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    //FileInputStream与FileOutputStream复制非文本文件
    public void test4() {
        //1、创建File类的对象,指明读入和写出的文件
        File srcFile = new File("F:\\Lean\\Text\\src\\main\\resources\\1.jpg");
        File destFile = new File("F:\\Lean\\Text\\src\\main\\resources\\2.jpg");
        //2、创建输入流和输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //3、复制过程
            //数组长度不是越大/越小就好,应当适中,一般为1024的倍数
            byte[] bytes = new byte[5];
            int len;//记录每次读取到数组的字节数
            while ((len = fis.read(bytes)) != -1) {
                //每次写出len个字节
                fos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭流
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值