96-Java的打印流、打印流重定向、补充知识:Properties、commons-io框架

一、打印流

  • 作用:打印流可以实现方便、高效的打印数据到文件中
  • 打印流一般是指:PrintStream、PrintWriter 两个类。
  • 可以实现打印什么数据就是什么数据
    • 例如:打印整数97写出去就是97,打印boolean的true,写出去就是true。

在这里插入图片描述




1、PrintStream
(1)构造器
构造器说明
public PrintStream(OutputStream out)打印流直接通向字节输出流管道
public PrintStream(File file)打印流直接通向文件对象
public PrintStream(String fileName)打印流直接通向文件路径
public PrintStream(String fileName, String csn)打印流直接通向文件路径,并指定字符集编码

(2)方法
方法名称说明
public void print(Xxx xx)打印任意类型的数据出去

package com.app.d6_print_stream;

import java.io.FileOutputStream;
import java.io.PrintStream;

/**
    目标:学会使用打印流,高效、方便的写数据出去
 */
public class PrintStreamDemo01 {
    public static void main(String[] args) {
        try (
                // 1、创建打印流管道接通目标文件
//                PrintStream ps = new PrintStream(new FileOutputStream("day11-io2-app/src/ps.txt"));
                PrintStream ps = new PrintStream("day11-io2-app/src/ps.txt");

                // 打印流还可以直接指定编码写出数据
//                PrintStream ps = new PrintStream("day11-io2-app/src/ps.txt", "GBK");
                ) {
            // 2、打印任意类型的数据
            ps.println(97);
            ps.println('a');
            ps.println(23.5);
            ps.println(true);
            ps.println("我是打印流输出的,我是啥就打印啥!");
            ps.println('国');
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述




2、PrintWriter
(1)构造器
构造器说明
public PrintWriter(OutputStream out)打印流直接通向字节输出流管道
public PrintWriter(Writer w)打印流直接通向字符输出流管道
public PrintWriter(File file)打印流直接通向文件对象
public PrintWriter(String fileName)打印流直接通向文件路径
public PrintWriter(String fileName, String csn)打印流直接通向文件路径,并指定字符集编码

(2)方法
方法名称说明
public void print(Xxx xx)打印任意类型的数据出去

package com.app.d6_print_stream;

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

/**
    目标:学会使用打印流,高效、方便的写出数据
 */
public class PrintWriterDemo02 {
    public static void main(String[] args) {
        try (
                // 1、创建打印流管道接通目标文件
//                PrintWriter pw = new PrintWriter(new FileWriter("day11-io2-app/src/pw.txt"));
                PrintWriter pw = new PrintWriter("day11-io2-app/src/pw.txt");   // 打印功能与PrintStream的使用没有区别

                // 打印流还可以直接指定编码写出数据
//                PrintWriter pw = new PrintWriter("day11-io2-app/src/pw.txt", "GBK");

                ) {

            // 2、打印任意类型的数据
            pw.println(97);
            pw.println('a');
            pw.println(23.5);
            pw.println(true);
            pw.println("我是打印流输出的,我是啥就打印啥!");
            pw.println('国');
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述




3、PrintStream与PrintWriter的区别
  • 打印数据功能上是一样的,都是使用方便、性能高效(核心优势)
  • PrintStream继承自字节输出流OutputStream,支持写字节数据的出去。
  • PrintWriter继承自字符输出流Writer,支持写字符数据的出去。



总结

1、打印流有几种?各有什么特点?

  • 打印流一般是指:PrintStream、PrintWriter两个类
  • 打印功能两者都是一样的使用方式
  • PrintStream继承自字节输出流OutputStream:支持写字节数据出去
  • PrintWriter继承自字符输出流Writer:支持写字符数据出去

2、打印流的优势什么?

  • 两者在打印功能上都是使用方便、性能高效(核心优势)



二、输出语句的重定向

  • 属于打印流的一种应用,可以把输出语句的打印位置改到文件。

    PrintStream ps = new PrintStream("文件地址");
    System.setOut(ps);
    

package com.app.d7_print_stream_redirect;

import java.io.PrintStream;

/**
    目标:了解打印流重定向操作:将系统打印流改成我们自己的打印流
 */
public class PrintStreamRedirectDemo {
    public static void main(String[] args) {
        System.out.println("风急天高猿啸哀,渚清沙白鸟飞回。"); // 这句语句底层就是调用打印流将数据打印到控制台的
        System.out.println("无边落木萧萧下,不尽长江滚滚来。");

        try (
                // 重定向:改变输出语句的位置到指定文件中
                PrintStream ps = new PrintStream("day11-io2-app/src/log.txt");
                ) {
            // 把系统打印流改成我们自己的打印流
            System.setOut(ps);

            System.out.println("万里悲秋常作客,百年多病独登台。");
            System.out.println("艰难苦恨繁霜鬓,潦倒新停浊酒杯。");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

控制台:

在这里插入图片描述


将系统打印流改成我们自己的打印流后:

在这里插入图片描述



三、补充知识:Properties

1、概述
  • Properties属性集对象
    • 其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用。

在这里插入图片描述



2、Properties核心作用
  • Properties代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中
    • 属性文件:后缀是.properties 结尾的文件,里面的内容都是key=value,后续做系统配置信息的。

3、Properties的API
  • Properties和IO流结合的方法:
方法名称说明
void load(InputStream inStream)从输入字节流读取属性列表(键和元素对)
void load(Reader reader)从输入字符流读取属性列表(键和元素对)
void store(OutputStream out, String comments)将此属性列表(键和元素对)写入此 Properties 表中,
以适合使用 load(InputStream)方法的格式写入输出字节流
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties 表中,
以适合使用 load(Reader)方法的格式写入输出字符流
public Object setProperty(String key, String value)保存键值对( put )
public String getProperty(String key)使用此属性列表中指定的键搜索属性值( get )
public Set< String > stringPropertyNames()所有键的名称的集合( keySet() )

package com.app.d8_properties;

import java.io.FileWriter;
import java.util.Properties;

/**
    目标:使用Properties把键值对信息存入到属性文件中(写入)
 */
public class PropertiesDemo01 {
    public static void main(String[] args) {
        // 1、创建属性集properties
        Properties properties = new Properties();

        // 2、将键值对信息存入到属性集properties中
        properties.setProperty("admin", "abc123");
        properties.setProperty("root", "123123");
        properties.setProperty("moyu", "6a6b6c");
        System.out.println(properties);

        try {
            /**
                结合IO流将属性集properties中的键值对数据信息存入到属性文件users.properties中
                参数一:保存管道(字符输出流)
                参数二:保存心得(注释)
             */
            properties.store(new FileWriter("day11-io2-app/src/users.properties")
                    , "This is users!!");

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
{root=123123, admin=abc123, moyu=6a6b6c}

Process finished with exit code 0

在这里插入图片描述



package com.app.d8_properties;

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

/**
    目标:使用Properties读取属性文件中的键值对信息(读取)
 */
public class PropertiesDemo02 {
    public static void main(String[] args) {
        // 1、创建属性集properties
        Properties properties = new Properties();
        System.out.println(properties); // {}:空的

        try {
            // 2、加载属性文件中的键值对数据信息到属性集properties中
            properties.load(new FileReader("day11-io2-app/src/users.properties"));
            System.out.println(properties);

            // 3、根据键获取属性值
            String rs1 = properties.getProperty("root");
            System.out.println(rs1);
            String rs2 = properties.getProperty("admin");
            System.out.println(rs2);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
{}
{root=123123, admin=abc123, moyu=6a6b6c}
123123
abc123

Process finished with exit code 0




总结

1、Properties的作用是啥?

  • 可以存储Properties属性集的键值对数据到属性文件中
    • void store(Writer writer, String comments)
  • 可以加载属性文件中的数据到属性集Properties中
    • void load(Reader reader)



四、补充知识:IO框架

1、commons-io概述
  • commons-io是apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发的效率。
  • commons-io工具包提供了很多有关IO操作的类。
  • 有两个主要的类:FileUtilsIOUtils


2、下载commons-io的jar包

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


重点关注FileUtils、IOUtils

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述



3、FileUtils主要方法
方法名称说明
String readFileToString(File file, String encoding)读取文件中的数据,返回字符串
void copyFile(File srcFile, File destFile)复制文件到某个文件夹
void copyDirectoryToDirectory(File srcDir, File destDir)复制文件夹到某个文件夹


4、导入commons-io-2.6.jar做开发
  • 需求
    • 使用commons-io简化IO流读写数据
  • 分析
    • 1、在项目中创建一个文件夹:lib
    • 2、将commons-io-2.6.jar文件复制到lib文件夹
    • 3、在jar文件上点右键,选择Add as Library(加载到依赖库中) ——> 点击OK
    • 4、在类中导包使用
  • 1、2、3步

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述




  • 4步

在这里插入图片描述

在这里插入图片描述



在这里插入图片描述



在这里插入图片描述



在这里插入图片描述


在这里插入图片描述


在这里插入图片描述



在这里插入图片描述



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值