20220520_打印字节流_打印字符流_Properties储存加载

打印字节流

public class printDemo1 {
    public static void main(String[] args) throws Exception {
        PrintStream pst = new PrintStream("CertExam\\src\\pstTest01.txt");
        /**
         * this(false, new FileOutputStream(fileName));
         * this内部如下
         * super(out);
         * this.autoFlush = autoFlush;
         * this.charset = out instanceof PrintStream ps ? ps.charset() : Charset.defaultCharset();
         * this.charOut = new OutputStreamWriter(this, charset);
         * this.textOut = new BufferedWriter(charOut);
         */
        pst.println(100);
        pst.println('a');
        pst.println('@');
        pst.println(false);
        pst.println(3333.3);
        pst.println("在文件中我可以输出中文");

        pst.close();
    }
}

打印字符流

public class printDemo2 {
    public static void main(String[] args) throws Exception {
        PrintWriter pwr = new PrintWriter("CertExam\\src\\pstTest01.txt");
        /**
         * this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),false);
         * 结构明了,缓冲->转换->原始字节流
         * FileWriter fw = new FileWriter("CertExam\\src\\pstTest01.txt");
         * 其实字符流内部也是用转换流包了一条字节流
         * 看来字节打印流PrintStream和字符打印流PrintWriter几乎没区别,打出去的东西完全一样
         */

        pwr.println(99);
        pwr.println('b');
        pwr.println("#");
        pwr.println(true);
        pwr.println(111.1);
        pwr.println("文件里中文输出没问题");

        pwr.close();

    }
}

properties的set和store

public class PropertiesDemo1 {
    public static void main(String[] args) throws IOException {
        Properties pro = new Properties();
        /**
         * Map集合,在属性集合这里,put和 setProperties的差别一
         * put的key,和value可以是任何object,而setProperty的键值都必须是String
         * 差别二
         * hashmap的value参数可以设置空值null,而当setProperty的value参数为null时,会抛出空指针异常NullPointerException
         */
        pro.setProperty("leon","12345");
        pro.setProperty("levis","23456");
        pro.setProperty("louis","34567");
        System.out.println(pro);
//        Map<String,Integer> imap = new HashMap<>();
//        imap.put("lily",0);
        /**
         * 储存时要new一个输出流,有多种选项,就用字符输出流
         */
        pro.store(new FileWriter("CertExam\\src\\Users.properties.txt"),
                "This is a first properties file");
    }
}

load

public class PropertiesDemo2 {
    public static void main(String[] args) throws IOException {
        /**配置文件properties的读取还是很方便的*/
        Properties pr = new Properties();
        pr.load(new FileReader("CertExam\\src\\Users.properties.txt"));
        System.out.println(pr);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值