Java学习:IO流

一、IO流所有知识点
在这里插入图片描述
二、代码演示
(1)字符流复制文本文件

public class CopyFile {
    public static void main(String[] args) throws IOException {
//        A:案例演示:	字符流复制文本文件
        // 频繁的读写操作

        InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("E:\\a.txt"));
        int ch=0; //记录读取到的字符
         //一次读取一个字符复制文件
        while((ch=in.read())!=-1){
            out.write(ch);
            out.flush();
        }
        // 一次读取一个字符数组复制文件
//        int len=0; //记录读取的有效字符的个数
//        char[] chars = new char[1024 * 8];
//        while((len=in.read(chars))!=-1){
//            out.write(chars,0,len);
//            out.flush();
//        }
        in.close();
        out.close();
    }
}

此代码包括一次复制一个字符和一次复制一个字符数组代码,还有一种高效流和便捷流,编程思想与上同。
字节流复制文件的思路与字节流相同,只是它总共两种,没有便捷流。
(2)Properties作为Map集合

public class demo2 {
    public static void main(String[] args) throws IOException {
//        B:案例演示
//        Properties的load()和store()功能
        Properties p = new Properties();
        p.setProperty("qq", "11");
        p.setProperty("mm", "22");
        p.setProperty("aa", "33");
        FileWriter f = new FileWriter("t.txt");
        p.store(f,"学生姓名和年龄信息");
        f.close();
        FileReader fr = new FileReader("t.txt");
        p.load(fr);
        String aa = p.getProperty("aa");
        System.out.println(aa);
        fr.close();

    }
}

有了它,可以用集合的方法读取文本文件的数据,比较方便。
三、总结
此处内容还是理解的不透彻,应该在继续加强。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值