字节流和字符流练习

字节流和字符流的使用场景:

字节流:拷贝任意类型的文件

字符流:读取纯文本文件中的数据        往纯文本文件中写出数据


练习1:

拷贝一个文件夹,考虑子文件夹
public class Test01 {
    public static void main(String[] args) throws IOException {
        //拷贝一个文件夹,考虑子文件夹

        //1.创建对象表示数据源
        File src = new File("D:\\aaa\\src");
        //2.创建对象表示目的地
        File dest = new File("D:\\aaa\\dest");

        //3.调用方法开始拷贝
        copydir(src,dest);

    }

    /*
    * 作用:拷贝文件夹
    * 参数一:数据源
    * 参数二:目的地
    *
    * */
    private static void copydir(File src, File dest) throws IOException {
        dest.mkdirs();
        //递归
        //1.进入数据源
        File[] files = src.listFiles();//如果是带有权限的文件,返回的是null,要对files进行非空判断
        //2.遍历数组
        for (File file : files) {
            if(file.isFile()){
                //3.判断文件,拷贝
                FileInputStream fis = new FileInputStream(file);//从文件中复制
                FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));//复制到文件中去
                byte[] bytes = new byte[1024];
                int len;//当前读取到了多少个字节
                while((len = fis.read(bytes)) != -1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }else {
                //4.判断文件夹,递归
                copydir(file, new File(dest,file.getName()));
            }
        }
    }
}

练习2:

为了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
加密原理:
    对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新的文件中。
解密原理:
    读取加密之后的文件,按照加密的规则反向操作,变成原始文件。
public class Test02 {
    public static void main(String[] args) throws IOException {
        /*
            为了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
            加密原理:
                对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新的文件中。
            解密原理:
                读取加密之后的文件,按照加密的规则反向操作,变成原始文件。
*/
        /*//加密
        FileInputStream fis=new FileInputStream("D:\\JavaTest\\myio\\myio\\src\\img.png");//创建对象关联原始文件
        FileOutputStream fos=new FileOutputStream("D:\\JavaTest\\myio\\myio\\src\\imgcopy.png");//创建对象关联加密文件
        //加密处理
        int b;
        while ((b=fis.read())!=-1){
            fos.write(b^2);
        }
        fos.close();
        fis.close();*/

        //解密
        FileInputStream fis=new FileInputStream("D:\\JavaTest\\myio\\myio\\src\\imgcopy.png");
        FileOutputStream fos=new FileOutputStream("D:\\JavaTest\\myio\\myio\\src\\redu.png");
        int b;
        while ((b=fis.read())!=-1){
            fos.write(b^2);
        }
        fos.close();
        fis.close();
        /*  ^ : 异或
                 两边相同:false
                 两边不同:true

                 0:false
                 1:true

               100:1100100
               10: 1010

               1100100
             ^ 0001010
             __________
               1101110
             ^ 0001010
             __________
               1100100
        */
    }
}

练习3:

文本文件中有以下的数据:
    2-1-9-4-7-8
将文件中的数据进行排序,变成以下的数据:
    1-2-4-7-8-9
public class Test03 {
    public static void main(String[] args) throws IOException {
        /*
            文本文件中有以下的数据:
                2-1-9-4-7-8
            将文件中的数据进行排序,变成以下的数据:
                1-2-4-7-8-9
        */

        //1.读取数据
        FileReader fr = new FileReader("myio\\a.txt");
        StringBuilder sb = new StringBuilder();//把读取到的数据拼接到sb中
        int ch;
        while((ch = fr.read()) != -1){
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);
        //2.排序
        String str = sb.toString();//先变成字符串,才能调用split方法
        String[] arrStr = str.split("-");//切割2-1-9-4-7-8得到219478是字符串不是数字

        ArrayList<Integer> list = new ArrayList<>();//用于存放遍历转换之后的整数
        for (String s : arrStr) {
            int i = Integer.parseInt(s);
            list.add(i);//添加到集合中
        }
        Collections.sort(list);
        System.out.println(list);
        //3.写出
        FileWriter fw = new FileWriter("myio\\a.txt");
        for (int i = 0; i < list.size(); i++) {
            if(i == list.size() - 1){
                fw.write(list.get(i) + "");
            }else{
                fw.write(list.get(i) + "-");
            }
        }
        fw.close();
    }
}

简洁版:

public class Test04 {
    public static void main(String[] args) throws IOException {
        /*
            文本文件中有以下的数据:
                2-1-9-4-7-8
            将文件中的数据进行排序,变成以下的数据:
                1-2-4-7-8-9
            细节1:
                文件中的数据不要换行
            细节2:
                bom头
        */
        //1.读取数据
        FileReader fr = new FileReader("myio\\a.txt");
        StringBuilder sb = new StringBuilder();
        int ch;
        while((ch = fr.read()) != -1){
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);
        //2.排序
        Integer[] arr = Arrays.stream(sb.toString()
                .split("-"))
                .map(Integer::parseInt)
                .sorted()
                .toArray(Integer[]::new);
        //3.写出
        FileWriter fw = new FileWriter("myio\\a.txt");
        String s = Arrays.toString(arr).replace(", ","-");//将数组变成字符串,再把","替换成"-"
        String result = s.substring(1, s.length() - 1);//截取
        fw.write(result);
        fw.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梧桐小玉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值