打印流:
- 作用:打印流可以实现方便、高效的打印数据到文件中去。打印流一般是:PrintStream,PrintWriter两个类。
 - 可以实现打印什么数据就是什么数据,例如打印整数97写出去就是97,打印boolean的true,写出去就是true。
 
 
代码演示如下:
public class PrintDemo1 {
    public static void main(String[] args) throws Exception{
        //1.创建一个打印流对象
        PrintStream ps =new PrintStream("io-app2/src/ps.txt");
        ps.println(97);
        ps.println('a');
        ps.println(23.3);
        ps.println(true);
        ps.println("我是打印流输出的,我是啥就打印啥!");
        ps.close();
    }
} 

PrintStream和PrintWriter的区别
- 打印数据功能上是一模一样的,都是使用方便,性能高效(核心优势)
 - PrintStream继承自字节输出流OutputStream,支持写字节数据的方法。
 - PrintWriter继承自字符输出流Writer,支持写字符数据出去。
 
1.打印流有几种?各有什么特点?
- 打印流一般是指:PrintStream,PrintWriter两个类。
 - 打印功能二者是一样的使用方式
 - PrintStream继承自字节输出流OutputStream,支持写字节
 - PrintWriter继承自字符输出流Writer,支持写字符
 
2.打印流的优势是什么?
- 两者在打印功能上都是使用方便,性能高效(核心优势)
 
输出语句重定向
- 属于打印流的一种应用,可以把输出语句的打印位置改到文件。
 
代码演示如下:
public class PrintDemo2 {
    public static void main(String[] args) throws Exception{
        System.out.println("锦瑟无端五十弦");
        System.out.println("一弦一柱思华年");
        // 改变输出语句的位置(重定向)
        PrintStream ps = new PrintStream("io-app2/src/log.txt");
        System.setOut(ps); // 把系统打印流改成我们自己的打印流
        System.out.println("庄生晓梦迷蝴蝶");
        System.out.println("望帝春心托杜鹃");
    }
}
 
 
Properties属性集对象
- 其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用。
 
Propertie核心作用:
- Properties代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中去。
 - 属性文件:后缀是.properties结尾的文件,里面的内容都是key=value,后续做系统配置信息的。
 
代码演示如下:
public class PropertiesDemo01 {
    public static void main(String[] args) throws Exception{
        //需求:使用Properties把键值对信息存入到属性文件中去。
        Properties properties = new Properties();
        properties.setProperty("admin","123456");
        properties.setProperty("tomato","654321");
        properties.setProperty("heima","itcast");
        System.out.println(properties);
        /**
           参数一:保存管道 字符输出流管道
           参数二:保存心得
         */
        properties.store(new FileWriter("io-app2/src/users.properties"),"Wow!this is a bug!");
    }
} 

代码演示如下:
public class PropertiesDemo02 {
    public static void main(String[] args) throws Exception{
        //需求:Properties读取属性文件中的键值对信息。(读取)
        Properties properties = new Properties();
        System.out.println(properties);
        // 加载属性文件中的键值对数据到属性对象properties中去
        properties.load(new FileReader("io-app2/src/users.properties"));
        System.out.println(properties);
        String rs = properties.getProperty("dlei");
        System.out.println(rs);
        String rs1 = properties.getProperty("admin");
        System.out.println(rs1);
    }
} 
 
Properties的作用?
1.可以存储Properties属性集的键值对数据到属性文件中去:
void store(Writer writer,String comments)
2.可以加载属性文件中的数据到Properties对象中来:
void load(Reader reader)
commins-io概述
- commons-io是apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发的效率。
 - commons-io工具包提供了很多有关io操作的类。有两个主要的类FileUtils,IOUtils
 

导入commons-io-2.6.jar做开发
需求
- 使用commons-io简化io流读写
 
分析
- 在项目中创建一个文件夹:lib
 - 在commons-io-2.6.jar文件复制到lib文件夹
 - 在jar文件上点右键,选择Add as Library -> 点击OK
 - 在类中导包使用
 
public class CommonsIODemo01 {
    public static void main(String[] args) throws Exception{
        //1.完成文件复制
        IOUtils.copy(new FileInputStream("H:\\A桌面\\mac.jpg"),new FileOutputStream("H:\\A桌面\\mac02.jpg"));
        //2.完成文件复制到某个文件夹下!
        FileUtils.copyFileToDirectory(new File("H:\\A桌面\\mac.jpg"),new File("H:/"));
        //3.完成文件夹复制到某个文件夹下!
        FileUtils.copyDirectoryToDirectory(new File("H:\\B学习资料"),new File("H:\\new"));
        FileUtils.deleteDirectory(new File("H:\\new"));
        //JDK1.7 自己也做了一些一行代码完成复制的操作:New IO的技术
        Files.copy(Path.of("H:\\A桌面\\mac.jpg"),Path.of("H:\\A桌面\\mac03.jpg"));
    }
}
                

















