自学内容网 自学内容网

Java重修笔记 第六十五天 IO 流 - 打印流、PrintStream 和 PrintWriter、properties 类

  • 打印流

1. 打印流只有输出流没有输入流

2. 字节打印流和字符打印流都是包装流,字节打印流是 FilterOutputStream 类的子类

3. 打印流不仅可以打印在显示器上,根据构造函数传进来的参数指定打印到不同对象中去

  • PrintStream 类(字节打印流)
public class PrintStream01 {
    public static void main(String[] args) throws IOException {
        // 字节打印流
        PrintStream out = System.out;
        // 默认情况下, 打印流输出数据的位置是显示器
        out.print("PrintStream Hello");
        out.write("韩顺平, 你好".getBytes(StandardCharsets.UTF_8));
        // 修改打印流打印的设备
        System.setOut(new PrintStream("d:\\a.txt"));
        // 修改默认输出设备后, 输出的就不是显示器, 而是规定的文件中
        System.out.println("hello, 韩顺平教育! ");
        out.close();
    }
}

  • PrintWriter 类(字符打印流)
public class PrintWriter01 {
    public static void main(String[] args) throws IOException {
        // PrintWriter printWriter = new PrintWriter(System.out);
        PrintWriter printWriter = new PrintWriter(new FileWriter("d:\\a.txt"));
        printWriter.println("PrintWriter hello");
        printWriter.close();
    }
}

  • Properties 类常用方法

1. load:加载输入流到 Properties 对象

        public void load(Reader reader) throws IOException

        参数:reader - 输入字符流

        public void load(InputStream inStream) throws IOException

        参数:inStream - 输入字节流

        异常:IOException - 从输入流读取时是否发生错误

                   IllegalArgumentException - 输入中是否出现格式错误的Unicode转义

2. list:将此属性列表打印到指定的输出流

        public void list(PrintWriter out)

        参数:out - 输出字符流

        public void list(PrintStream out)

        参数:out - 输出字节流

        异常:ClassCastException - 如果此属性列表中有 key 不是字符串

3. getProperty:根据 key 获取 value

        public String getProperty(String key)

        返回值:key 对应的 value 值,若文件中没有对应的键值对,则返回 null

        参数:key - 键

public class Properties01 {
    public static void main(String[] args) throws IOException {
        // 使用 Properties 类来读取 mysql.properties 文件, 获得该文件下的所有属性信息
        // 第一步: 创建 Properties 对象
        Properties properties = new Properties();
        // 第二步: 加载指定的配置文件
        properties.load(new FileReader("src\\mysql.properties"));
        // 第三步: 读取配置文件中的信息
        properties.list(System.out);
        // 第四步: 处理信息, 根据 key 获取对应的 value
        System.out.println("用户名 = " + properties.getProperty("user"));
        System.out.println("密码 = " + properties.getProperty("pwd"));
    }
}

4. 配置键值对

        public Object setProperty(String key, String value)

        返回值:此属性列表中指定键的上一个值,如果没有则返回 null 

        参数:key - 要放入此属性列表的关键字  

                   value - key 对应的 value 值

        说明:底层调用的是 HashTable 的 put() 方法,不过将 key 和 value 的类型限制为 String 

5. 存储配置文件

        public void store(Writer writer, String comments) throws IOException

        参数:writer - 输出字符流

                   comments - 属性列表的描述,会打印到该配置文件的第一行

        public void store(OutputStream out, String comments) throws IOException

        参数:out - 输出字节流

                   comments - 属性列表的描述,会打印到该配置文件的第一行

        说明:若文件不存在,则根据输出流指定的位置生成一个文件,文件内容是 setProperty 方法中添加的键值对,当键值对中出现中文时,会以 Unicode 码的形式保存(字节输入流)。当文件存在时,key 值相同的键值对会修改其对应的 value 值,否则就是添加键值对。

        异常:IOException - 如果将此属性列表写入指定的输出流 

                  ClassCastException - 如果此 Properties 对象包含的键值对类型不是 String

                  NullPointerException - 如果 out 为空

public class Properties02 {
    public static void main(String[] args) throws IOException {
        // 使用 Properties 来创建配置文件, 并修改文件里面的内容
        Properties properties = new Properties();
        // 配置键值对
        properties.setProperty("charset", "utf8");
        properties.setProperty("user", "汤姆"); // 若使用字节输入流保存中文时, 保存的是该中文的 Unicode 码
        properties.setProperty("pwd", "abc123");
        // 将配置好的键值对存储到指定文件中
        properties.store(new FileOutputStream("src\\mysql2.properties", true), "this is a description~");
        System.out.println("保存配置文件成功");
    }
}

原文地址:https://blog.csdn.net/Code___slave/article/details/142800664

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!