一、打印流
作用:打印流可以实现方便、高效的打印数据到文件中。打印流一般是指:PrintStream、PrintWriter 两个类。可以实现打印什么数据就是什么数据 例如:打印整数97写出去就是97,打印boolean的true,写出去就是true。1、PrintStream
(1)构造器
(2)方法
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)构造器
(2)方法
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流结合的方法:package com.app.d8_properties;import java.io.FileWriter;import java.util.Properties;/**目标:使用Properties把键值对信息存入到属性文件中(写入)*/public class PropertiesDemo01 {public static void main(String[] args) {// 1、创建属性集propertiesProperties 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、创建属性集propertiesProperties 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}123123abc123Process 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操作的类。有两个主要的类:FileUtils
、IOUtils
。2、下载commons-io的jar包
重点关注FileUtils、IOUtils
3、FileUtils主要方法
4、导入commons-io-2.6.jar做开发
需求: 使用commons-io简化IO流读写数据分析: 1、在项目中创建一个文件夹:lib
2、将commons-io-2.6.jar
文件复制到lib
文件夹3、在jar文件上点右键,选择Add as Library(加载到依赖库中)
——> 点击OK4、在类中导包使用1、2、3步:4步: