Java面向对象之打印流-PrintStream 和 PrintWriter及Properties 类

1、打印流-PrintStream 和 PrintWriter

1.1、一览图

打印流只有输出流,没有输入流

在这里插入图片描述
在这里插入图片描述

1.2、代码实现
package transformation;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * PrintWriter 使用方式
 */
public class PrintWriter_ {
    public static void main(String[] args) throws IOException {
        PrintWriter printWriter = new PrintWriter(System.out);
        System.out.println(printWriter);  // java.io.PrintWriter@5cad8086
        PrintWriter printWriter1 = new PrintWriter(new FileWriter("e:\\f2.txt"));
        System.out.println(printWriter1);  // java.io.PrintWriter@6e0be858
    }
}
package printstream;

import java.io.IOException;
import java.io.PrintStream;

/**
 * PrintStream (字节打印流/输出流)
 */
public class PrintStream_ {
    public static void main(String[] args) throws IOException {
        PrintStream out = System.out;
        // 在默认情况下, PrintStream 输出数据的位置是 标准输出, 即显示
        out.print("john, hello\n");
        /*
        print 源码
        public void print(String s) {
            if (s == null) {
                s = "null";
            }
            write(s);
        }
        */
        // 因为 print 底层使用的是 write, 所以我们可以直接调用 write 进行打印/输出
        out.write("jack, 你好".getBytes());
        out.close();

        /*
         我们可以去修改打印流输出的位置/设备
         1. 输出修改成到 "e:\\f1.txt"
         2. "hello, lily" 就会输出到 e:\f1.txt
         3. public static void setOut(PrintStream out) {
                checkIO();
                setOut0(out);  // native 方法, 修改了 out
            }
         */

        System.setOut(new PrintStream("e:\\f1.txt"));
        System.out.println("hello, lily");  // 将信息写入文件
    }
}

2、Properties 类

2.1、需求引出

在这里插入图片描述
在这里插入图片描述

2.2、基本介绍

在这里插入图片描述
在这里插入图片描述

2.3、应用案例

在这里插入图片描述
Properties01.java

package properties_;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Properties01 {
    public static void main(String[] args) throws IOException {
        // 读取 mysql.properties 文件, 并得到 ip, user 和 pwd
        BufferedReader br = new BufferedReader(new FileReader("e:\\mysql.properties"));
        String line = "";
        while ((line = br.readLine()) != null) {  // 循环读取
            String[] split = line.split("=");
            // 如果我们要求指定的 ip 值
            if ("ip".equals(split[0])) {
                System.out.println(split[0] + "值是: " + split[1]);
            }
        }
        br.close();
    }
}

mysql.properties.java

ip=192.168.100.100
user=root
pwd=12345
package properties_;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Properties02 {
    public static void main(String[] args) throws IOException {
        // 使用 Properties 类来读取 mysql.properties 文件
        // 1. 创建 Properties 对象
        Properties properties = new Properties();
        // 2. 加载指定配置文件
        properties.load(new FileReader("e:\\mysql.properties"));
        // 3. 把 k-v 显示控制台
        properties.list(System.out);
        // 4. 根据 key 获取对应的值
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println("用户名=" + user);
        System.out.println("密码是=" + pwd);
    }
}

控制台输出

-- listing properties --
user=root
pwd=12345
ip=192.168.100.100
用户名=root
密码是=12345
package properties_;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Properties03 {
    public static void main(String[] args) throws IOException {
        // 使用 Properties 类来创建 配置文件, 修改配置文件内容
        Properties properties = new Properties();
        // 创建
        // 1.如果该文件没有 key 就是创建
        // 2.如果该文件有 key 就是修改
        /*
        // Properties 父类是 Hashtable, 底层就是 Hashtable 核心方法
        public synchronized V put(K key, V value) {
            // Make sure the value is not null
            if (value == null) {
                throw new NullPointerException();
            }

            // Makes sure the key is not already in the hashtable.
            Entry<?,?> tab[] = table;
            int hash = key.hashCode();
            int index = (hash & 0x7FFFFFFF) % tab.length;
            @SuppressWarnings("unchecked")
            Entry<K,V> entry = (Entry<K,V>)tab[index];
            for(; entry != null ; entry = entry.next) {
                if ((entry.hash == hash) && entry.key.equals(key)) {
                    V old = entry.value;
                    entry.value = value;  // 如果 key 存在, 就替换
                    return old;
                }
            }

            addEntry(hash, key, value, index);  // 如果是新 k, 就 addEntry
            return null;
        }
        */
        properties.setProperty("charset", "utf8");
        properties.setProperty("user", "汤姆");  // 注意保存时, 是中文的 unicode 码值
        properties.setProperty("pwd", "888888");
        // 将 k-v 存储文件中即可
        properties.store(new FileOutputStream("e:\\mysql2.properties"), null);
        System.out.println("保存配置文件成功~");
    }
}

控制台输出

#Sat Jan 08 21:33:44 CST 2022
user=\u6C64\u59C6
pwd=888888
charset=utf8

3、练习

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值