字符流:
转换流:
字符输入流
- 字符输入流转换流InputStreamReader作用
- 构造方法 public InputStreamReader(InputStream is,String charset);
- 可以解决由不同编码的文件读取时,解决乱码问题
- 可以指定编码把原始字节流转换成字符流,如此字符流中的字符不会乱码
代码:
package com.hhm.h_tranfer_stream;
import java.io.*;
/* 转换流
*
* */
public class CharSetTest001 {
public static void main(String[] args) {
/*
*
* 文件是GBK IDEA是UTF-8
* */
try (
//资源流的对象
//1、创建一个输入流
/*转换流 :InputStreamReader public InputStreamReader(InputStream is) */
InputStream in = new FileInputStream("F:\\testFile\\out.txt");
//把原始字节流转换为字符输入流
InputStreamReader ir = new InputStreamReader(in, "GBK");
) {
int len;
while ((len = ir.read()) != -1) {
System.out.print((char) len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符输出流
字符输出流OutputStreamWriter
- 构造方法OutputStreamWriter(OutputStream os,String charset);
- 可以指定编码把字节输出流转换成字符输出流,从而可以指定写出去的字符编码
代码:
import java.io.*;
/* 转换流
* 字符输出流
* */
public class CharSetOut {
public static void main(String[] args) throws Exception {
//定义字节输出流
OutputStream os = new FileOutputStream("F:\\testFile\\out.txt",true);
OutputStreamWriter ow = new OutputStreamWriter(os,"GBK");
//把低级的流 再做一次包装 做成高级缓冲流
BufferedWriter bw = new BufferedWriter(ow);
bw.write("OutputStreamWriter输出流");
bw.close();
}
}
对象序列化
- 作用:以内穿为基准,把内存中的对象存储到磁盘文件中去,称为对象序列化。
- 构造方法:public ObjectOutputStream(OutputStream out);把低级字节流包装成高级对象字节流输出流
- 将对象写入的磁盘当中
- 对象字节输出流,写对象数据到磁盘
- 使用到的流是对象字节输出流:ObjectOutputStream
使用序列化存储对象,对象类必须实现Serializable接口否则,就会抛出一个没有序列化的错误
public static void main(String[] args) {
//1、创建一个学生类对象
Student s = new Student("张国荣",25);
Student s1 = new Student("张学友",25);
Student s2 = new Student("陈冠希",25);
try (
//2、创建低级流
OutputStream os = new FileOutputStream("test.txt");
//3、对象序列化:使用对象字节输出流包装字节输出流管道
ObjectOutputStream oos = new ObjectOutputStream(os);
){
//3、直接调用序列化方法 想调用序列化方法必须让Student类实现一个接口
oos.writeObject(s);
//释放资源
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Student类:
public class Student implements Serializable {
private String name;
private transient Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
总结:
序列化的含义是什么?
- 把对象存储到文件中去
- 对象序列化用到了哪个流?
- 对象字节输出流ObjectOutputStream
- public void writeObject(Object obj)
序列化对象的要求是怎么样的?
使用到的流是对象字节输入流:ObjectInputStream
- 作用:以内存为基准,把存储到磁盘文件中去的对象数据恢复成内存中的对象,称为对象反序列化
- 构造器:public ObjectInputStream(InputStream in);把低级字节流包装成高级对象字节输入流
- 序列化方法:public Object readObject();把存储在磁盘文件中的对象数据恢复成内存中的对象返回
transient这个修饰符是不让这个属性写入序列化文件
public class ObjcetInputStreamDemo01 {
public static void main(String[] args) {
try (
//2、创建低级流
InputStream os = new FileInputStream("test.txt");
//3、对象序列化:使用对象字节输入流包装字节输出流管道
ObjectInputStream oi = new ObjectInputStream(os);
){
//3、直接调用反序列化方法 单个序列化取出方法 我在里面存了三个序列化对象 调用了三次
Student st = (Student) oi.readObject();
Student st1 = (Student) oi.readObject();
Student st2 = (Student) oi.readObject();
System.out.println(st.getName()+" "+st.getAge());
System.out.println(st1.getName()+" "+st1.getAge());
System.out.println(st2.getName()+" "+st2.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
student类:
public class Student implements Serializable {
private String name;
//transient修饰的成员变量不参与序列化
private transient Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
打印流
作用:打印流可以实现方便、高效的打印数据到文件中去。打印流一般是指:PrintStream,PrintWriter两个类
- 可以实现打印说明数据就是什么数据
构造器:
- public PrintStream(OutputStream os);打印流直接通向字节输出流管道
- public PrintStream(File f);打印流直接通向文件对象
- public PrintStream(String filepath);打印流直接通向文件路径
- 打印流构造器支持编码
如果要做追加操作就用第一个构造器
package com.hhm.h_print_stream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/*
*
* 学会使用PrintStream 打印流 高效 方便
* */
public class PrintStreamDemo {
public static void main(String[] args) {
//1、创建一个打印流对象
try (
PrintStream pr = new PrintStream("test.txt");
) {
//内容写什么 文件就得到什么
pr.print(97);
pr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
PrintWriter
构造器:
- public PrintStream(OutputStream os);打印流直接通向字节输出流管道
- public PrintStream(File f);打印流直接通向文件对象
- public PrintStream(String filepath);打印流直接通向文件路径
PrintWriter与PrintStream区别
- 打印数据功能上是一模一样,都是使用方便性能高效(核心优势)
- PrintStream继承自字节输出流OutputStream,支持写字节数据的方法
- PrintWriter继承自字符输出流Writer,支持字符数据
- 如果要使用写数据writer()方法 就只能使用PrintWriter,没有必要毕竟这是打印流
改变输出语句的位置到文件
//重定向
class PrintDemo2{
//print原理就是调用的就是打印流
//System.out.println();
public static void main(String[] args){
System.out.println("锦瑟无端五十弦"); //流到控制台
System.out.println("一弦一柱思华年"); //流到控制台
//我们使用打印流改变输出语句的位置
PrintStream ps = new PrintStream("test.txt");
System.setOut(ps); //把系统打印流改成我们自己的打印流
System.out.println("庄生晓梦迷蝴蝶"); //流到文件中
System.out.println("望帝春心托杜鹃"); //流到文件中
}
}
Properties
- 其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用
Properties核心作用
- 代表的是一个属性文件,可以把自己2对象中的键值对信息存入到一个属性文件中去
- 属性文件:后缀是.properties结尾的文件,里面内容都是key=value,后续做系统配置信息
- 构造器:properties();
存储
public class h_propertiesDemo {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
// properties.put("admin","123456");
//不使用put方法 使用setProperty方法
properties.setProperty("admin","123456");
//存储到文件当中去
//可以存储到低级的字符输出流里面 也可以选择存入低级字符输出流里面 两种没有区别
/*
* 参数一:流管道
* 参数二:可写可不写 一个注释 comments
* */
properties.store(new FileWriter("test.properties"),"give me 300 thank you ");
}
}
读取
public class PropertiesLoad {
/*
* 加载数据
*
* */
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileReader("test.properties"));
System.out.println(properties);
}
}
properties中的get方法单独取数据
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileReader("test.properties"));
//当properties文件中有多个 key和值我们就得取 properties提供了get方法直接get键
//无非就调用的是get 但是他帮你重写了所以可以得到String类型
String admin = properties.getProperty("admin");
System.out.println(admin);
}
}
实现原理:
从文件中获取到数据,然后放到properties这个集合当中然后输出
IO框架
Commons-io概述
- Commons-io是Apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发效率。
commons-io工具包提供了很多有关IO的操作类。有两个主要的类FileUtils,IOUtils
public class CommonsIoDemo01 {
public static void main(String[] args) throws IOException {
//1、完成文件复制到某一个文件夹下
FileUtils.copyFileToDirectory(new File("F:\\testFile\\test.txt"),new File("F:"));
//2、完成文件复制
IOUtils.copy(new FileInputStream("F:\\testFile\\test.txt"),
new FileOutputStream("F:\\testFile\\test2.txt"));
//3、完成文件夹复制到某个文件下
FileUtils.copyDirectoryToDirectory(new File("F:\\testFile"),new File("D:\\newTestFile"));
//4、删除某个文件夹
FileUtils.deleteDirectory(new File("F:\\newTestFile"));
}
}
JDK1.7原生的 后newIo 新Io
//完成文件的复制
Files.copy(Path.of("F:\\testFile\\test.txt"),Path.of("F:\\testFile\\test2.txt"));
//只能删空文件夹 第三方可以删除有文件的文件夹
Files.deleteIfExists(Path.of("F:\\testFile"));