java io流 总结⑥

字节缓冲流与字符缓冲流的练习题(共两题)

第一题:把《出师表》的文章顺序进行恢复到一个新文件中。

public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("io\\csb.txt"));
        String line;
        ArrayList<String> arrayList = new ArrayList<>();
        while ((line = br.readLine()) != null){
            arrayList.add(line);
        }
        br.close();
        //排序
        Collections.sort(arrayList, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int i1 = Integer.parseInt(o1.split("\\.")[0]);
                int i2 = Integer.parseInt(o2.split("\\.")[0]);
                //使用split进行切割 以.为切割符 把前面的数字提取出来 用作排序
                return i1 - i2;
            }
        });
        //写出
        BufferedWriter bw = new BufferedWriter(new FileWriter("io\\bbb.txt"));
        for (String s : arrayList) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
}
方法二:在排序时使用treemap 底层hashmap
public static void main(String[] args) throws IOException {
        /*
                需求:把《出师表》的文章顺序进行恢复到一个新文件中。
            */
        BufferedReader br = new BufferedReader(new FileReader("D:\\io\\csb.txt"));
        String line;
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        while ((line = br.readLine()) != null) {
            String[] split = line.split("\\.");
            treeMap.put(Integer.parseInt(split[0]), split[1]);
            //键为前面的序号 值为后面的文字
        }
        System.out.println(treeMap);
        br.close();
​
        //写出数据
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\io\\bbb.txt"));
        方法一:
        /*for (int i = 1; i < treeMap.size(); i++) {
            bw.write(treeMap.get(i));
            bw.newLine();
        }
        bw.close();*/
        
        方法二:
        Set<Map.Entry<Integer, String>> entries = treeMap.entrySet();
        //获取每一个键值对对象
        for (Map.Entry<Integer, String> entry : entries) {
            bw.write(entry.getValue());
            bw.newLine();
        }
            bw.close();
​
    }
}
第二题:实现一个验证程序运行次数的小程序,要求如下:
1.当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~
2.程序运行演示如下:
    第一次运行控制台输出: 欢迎使用本软件,第1次使用免费~
    第二次运行控制台输出: 欢迎使用本软件,第2次使用免费~
    第三次运行控制台输出: 欢迎使用本软件,第3次使用免费~
    第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~
public static void main(String[] args) throws IOException {
        //1.把文件中的数字读取到内存中
        //原则:
        //IO:随用随创建
        //    什么时候不用什么时候关闭
        BufferedReader br = new BufferedReader(new FileReader("myio\\count.txt"));
        String line = br.readLine();
        br.close();
​
        System.out.println(line);//null
        int count = Integer.parseInt(line);
        //表示当前软件又运行了一次
        count++;//1
        //2.判断
        if(count <= 3){
            System.out.println("欢迎使用本软件,第"+count+"次使用免费~");
        }else{
            System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("myio\\count.txt"));
        //3.把当前自增之后的count写出到文件当中
        bw.write(count + ""); //97 + ""
        //此处需要把写入文件中的数字转为字符 因为当你写入数字时会自动转化为ascii码中的值
        //例如此处写97转到文件中就为 a
        //若真的要写97 那个 97+"" 转化为字符 则写入文件的就为97
        bw.close();
​
    }
}
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

皎月当空照我素心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值