目录
知识点
1.IO流
2.IO流的分类
3.FileOutputStream
4.FileInputStream
5.拷贝文件
6.try{...}catch{...}finally{...}和AutoCloseable接口
7.字符集
8.乱码产生原因
9.如何不产生乱码
10.java中编码解码的方法
11.FileReader
12.FileWriter
13.字符流底层原理
14.字节缓冲流
15.字符缓冲流
16.转换流
17.序列化流和反序列化流
18.打印流
19.压缩流和解压缩流
20.Commons-io
21.Hutool工具包
代码实现
1.FileOutputStream写入文件
2.FileOutputStream写入文件(利用byte[]数组)
3.FileOutputStream写入文件(续写)
4.FileInputStream读取文件(循环读取)
5.拷贝文件(循环读写)
6.默认编码和指定编码
7.FileReader读取字符
8.FileWriter写入字符
9.字节缓冲流
10.字符缓冲流
11.转换流(指定字符编码读写)
12.转换流(指定字符编码转换)
13.转换流(字节流读取整行)
14.序列化流和反序列化流
15.打印流
16.压缩流和解压缩流
存储和读取数据的解决方案
用于读写文件中的数据(可以读写文件,或网络中的数据)
流的方向:输入流和输出流
操作文件类型:字节流(操作所有文件)和字符流(纯文本文件//记事本可打开且可读懂)
创建字节输出流对象、写入数据(覆盖/续写)、释放资源
创建字节输入流对象、读取数据(文件末尾-1)、释放资源
利用Byte[]数组多字节拷贝提高效率
finally{...}语句一定会执行,除非JVM退出
try(实现了AutoCloseable接口的对象){...}catch{...},自动释放资源
ASCII字符集(英文):一个字节,高位0开头
GB 2312字符集(简体)
BIG5字符集(繁体)
GBK字符集(中文window系统默认):汉字两个字节,高位1开头,兼容ASCII字符集
Unicode字符集(万国码):UTF-8编码规则(1~4字节),ASCII一个字节高位0开头,汉字三个字节高位1开头
读取数据时未读完整个汉字
编码和解码方式不统一
不要用字节流读取文本文件
编码解码时使用同一个码表,同一个编码方式
String类的方法 | 说明 |
---|---|
public byte[] getBytes() | 使用默认方式进行编码 |
public byte[] getBytes(String charsetName) | 使用指定方式进行编码 |
String(byte[] bytes) | 使用默认方式进行解码 |
String(byte[] bytes,String charsetName) | 使用指定方式进行解码 |
创建字符输入流对象,读取数据(文件末尾-1),释放资源
创建字符输出流对象,写入数据(覆盖/续写),释放资源
创建字符输入/输出流对象:关联文件、创建缓冲区(8192字节数组)
读取数据:缓冲区空,从文件读满;缓冲区非空,从缓冲区读取
写入数据:缓冲区满,自动写入文件;flush()手动写入文件;close()手动写入文件并关闭流
BufferedInputStream、BufferedOutputStream
把基本流包装成高级流,提高读取/写入数据的性能(底层自带8192字节缓冲区)
BufferedReader、BufferedWriter
把基本流包装成高级流,提高读取/写入数据的性能(底层自带8192字符缓冲区)
readLine()//整行读取
newLine()//跨平台换行
InputStreamReader、OutputStreamWriter
是字符流和字节流之间的桥梁
作用:字节流使用字符流中的方法、指定编码方式读写
ObjectInputStream、ObjectOutputStream
可以把Java中的对象写到本地文件中
Javabean类实现Serializable接口、序列化文件不能修改、serialVersionUID作为Javabean类序列化、transient关键字不序列化成员变量
PrintStream,PrintWriter
打印流只操作文件目的地,不操作数据源
特有的写出方法实现数据原样写出、自动刷新换行
System.out是标准输出流,由虚拟机启动,默认指向控制台
解压原理:把每一个ZipEntry按照层级拷贝到本地另一个文件夹中
压缩原理:把每一个文件或文件夹当做ZipEntry对象放到压缩包中
apache开源基金组织提供的一组有关IO操作的开源工具包
作用:提高IO流的开发效率
官网:https://hutool.cn/
API文档:https://apidoc.gitee.com/dromara/hutool/
中文使用文档:https://hutool.cn/docs/#/
package IOtest1;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest1\\a.txt");
fos.write(97);//覆盖字符写入
fos.close();//释放资源
}
}
package IOtest2;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest2\\a.txt");
byte[] bytes={97,98,99,100,101};//byte数组
fos.write(bytes);
fos.write(bytes,1,3);
fos.close();
}
}
package IOtest3;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos1=new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest3\\a.txt");
byte[] bytes1="abc\r\n6556\r\n".getBytes();//回车换行\r\n
fos1.write(bytes1);
fos1.close();
FileOutputStream fos2=new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest3\\a.txt",true);//文件续写
byte[] bytes2="continue".getBytes();
fos2.write(bytes2);
fos2.close();
}
}
package IOtest4;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileInputStream fis1=new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest4\\a.txt");
int b1=fis1.read();//单字节读写
System.out.println((char) b1);
int b2=fis1.read();
System.out.println((char) b2);
int b3=fis1.read();
System.out.println((char) b3);
int b4=fis1.read();
System.out.println(b4);//文件末尾是-1
fis1.close();
FileInputStream fis2=new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest4\\a.txt");
int b;
while((b=fis2.read())!=-1){//循环读写
System.out.print((char)b);
}
fis2.close();
}
}
package IOtest5;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
long start1=System.currentTimeMillis();//拷贝开始时间
FileInputStream fis1 = new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest5\\mellco-img1.jpg");
FileOutputStream fos1 = new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest5\\mellco-img1_copy1.jpg");
int b;
while ((b = fis1.read()) != -1) {//边读取边写入(单个字节)
fos1.write(b);
}
fos1.close();
fis1.close();
long end1=System.currentTimeMillis();//拷贝结束时间
System.out.println("单字节拷贝用时"+(end1-start1)+"ms");
long start2=System.currentTimeMillis();//拷贝开始时间
FileInputStream fis2 = new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest5\\mellco-img1.jpg");
FileOutputStream fos2 = new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest5\\mellco-img1_copy2.jpg");
byte[] bytes=new byte[64];
int len;
while ((len= fis2.read(bytes))!=-1){//边读取边写入(字节数组)
fos2.write(bytes,0,len);//未读满时bytes[len]后舍去
}
fos2.close();
fis2.close();
long end2=System.currentTimeMillis();//拷贝结束时间
System.out.println("单字节拷贝用时"+(end2-start2)+"ms");
}
}
package IOtest6;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class IODemo {
public static void main(String[] args) throws IOException {
String str="爱你哟";
//默认编码
byte[] bytes1=str.getBytes();//IDEA默认UTF-8编码方式
System.out.println(Arrays.toString(bytes1));
//指定编码
byte[] bytes2=str.getBytes("GBK");
System.out.println(Arrays.toString(bytes2));
//默认解码
System.out.println(new String(bytes1));//IDEA默认UTF-8编码方式
//指定解码
System.out.println(new String(bytes2, "GBK"));
System.out.println(new String(bytes2,"UTF-8"));
}
}
package IOtest7;
import java.io.FileReader;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileReader fr1 = new FileReader("G:\\JavaProgram\\java\\src\\IOtest7\\a.txt");
int ch;
while ((ch = fr1.read()) != -1) {//循环读取单个字符
System.out.print((char)ch);
}
fr1.close();
System.out.println();
FileReader fr2 = new FileReader("G:\\JavaProgram\\java\\src\\IOtest7\\a.txt");
char[] chars=new char[2];
int len;
while ((len=fr2.read(chars))!=-1){//循环读取字符数组
System.out.print(new String(chars, 0, len));
}
fr2.close();
}
}
package IOtest8;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IODemo {
public static void main(String[] args) throws IOException {
FileWriter fw1 = new FileWriter("G:\\JavaProgram\\java\\src\\IOtest8\\a.txt");
fw1.write("创建字符输入流对象\n");
fw1.close();
FileWriter fw2 = new FileWriter("G:\\JavaProgram\\java\\src\\IOtest8\\a.txt",true);//开启续写
fw2.write("读取数据");
fw2.close();
}
}
package IOtest9;
import java.io.*;
public class IODemo {
public static void main(String[] args) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest9\\a.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest9\\copy.txt"));
int b;
while ((b=bis.read())!=-1){
bos.write(b);
}
bos.close();//只关缓冲流、不管基本流
bis.close();
BufferedInputStream bis2=new BufferedInputStream(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest9\\a.txt"));
BufferedOutputStream bos2=new BufferedOutputStream(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest9\\copy2.txt"));
byte[] bytes=new byte[1024];
int len;
while((len=bis2.read(bytes))!=-1){
bos2.write(bytes,0,len);
}
bos2.close();
bis2.close();
}
}
package IOtest10;
import java.io.*;
public class IODemo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("G:\\JavaProgram\\java\\src\\IOtest10\\a.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("G:\\JavaProgram\\java\\src\\IOtest10\\copy.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
bw.write(line);
bw.newLine();
}
bw.close();//只关缓冲流、不管基本流
br.close();
BufferedWriter bw2=new BufferedWriter(new FileWriter("G:\\JavaProgram\\java\\src\\IOtest10\\copy.txt",true));
bw2.write("续写");
}
}
package IOtest11;
import java.io.*;
import java.nio.charset.Charset;
public class IODemo {
public static void main(String[] args) throws IOException {
//转换流指定字符编码读取(before jdk11)
InputStreamReader isr=new InputStreamReader(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest11\\a.txt"),"GBK");
int ch1;
while ((ch1=isr.read())!=-1){
System.out.print((char) ch1);
}
isr.close();
//转换流指定字符编码写出(before jdk11)
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest11\\b.txt"),"GBK");
osw.write("你好");
osw.close();
//字节流指定字符编码读取
FileReader fr=new FileReader("G:\\JavaProgram\\java\\src\\IOtest11\\a.txt", Charset.forName("GBK"));
int ch3;
while ((ch3=fr.read())!=-1){
System.out.print((char) ch3);
}
fr.close();
//字节流指定字符编码写出
FileWriter fw=new FileWriter("G:\\JavaProgram\\java\\src\\IOtest11\\b.txt",Charset.forName("GBK"));
fw.write("你好你好");
fw.close();
}
}
package IOtest12;
import java.io.*;
import java.nio.charset.Charset;
public class IODemo {
public static void main(String[] args) throws IOException {
//转换流转换指定编码(before jdk11)
InputStreamReader isr=new InputStreamReader(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest12\\a.txt"),"GBK");
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest12\\b.txt"),"UTF-8");
int b;
while ((b=isr.read())!=-1){
osw.write(b);
}
osw.close();
isr.close();
//字符流转换指定编码
FileReader fr=new FileReader("G:\\JavaProgram\\java\\src\\IOtest12\\a.txt",Charset.forName("GBK"));
FileWriter fw=new FileWriter("G:\\JavaProgram\\java\\src\\IOtest12\\b.txt",Charset.forName("UTF-8"));
int b2;
while((b2=fr.read())!=-1){
fw.write(b2);
}
fw.close();
fr.close();
}
}
package IOtest13;
import java.io.*;
import java.nio.charset.Charset;
public class IODemo {
public static void main(String[] args) throws IOException {
//转换流包裹字节流,字符缓冲流包裹转换流
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest13\\a.txt")));
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
}
package IOtest14;
import java.io.*;
import java.util.ArrayList;
public class IODemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化流(对象操作输出流)
Student student1 = new Student("张三", 18, "南京");
Student student2 = new Student("小红", 17, "北京");
Student student3 = new Student("小明", 17, "重庆");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest14\\a.txt"));
ArrayList list1=new ArrayList<>();
list1.add(student1);
list1.add(student2);
list1.add(student3);
oos.writeObject(list1);//序列化对象集合
oos.close();
//反序列化流(对象操作输入流)
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("G:\\JavaProgram\\java\\src\\IOtest14\\a.txt"));
ArrayList list2 = (ArrayList) ois.readObject();//反序列化对象集合
System.out.println(list2.toString());
ois.close();
}
}
package IOtest14;
import java.io.Serial;
import java.io.Serializable;
public class Student implements Serializable {//Serializable可序列化标记接口
private static final long serialVersionUID = -1654487619885343330L;
private String name;
private int age;
private transient String address;//瞬态关键字(不序列化成员变量)
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
package IOtest15;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class IODemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//字节打印流
PrintStream ps=new PrintStream(new FileOutputStream("G:\\JavaProgram\\java\\src\\IOtest15\\a.txt"),true, Charset.forName("UTF-8"));
ps.println(666);//写出+自动刷新+自动换行
ps.print(true);//写出
ps.printf("%d%s",1,"是奇数");//写出+占位符
ps.close();
//字符打印流
PrintWriter pw=new PrintWriter(new FileWriter("G:\\JavaProgram\\java\\src\\IOtest15\\b.txt"),true);
pw.println("你好你好你好你好");
pw.print("我见过你");
pw.printf("%d%s",2,"是偶数");
pw.close();
//系统标准输出流,由虚拟机启动,默认指向控制台,关闭后重新打开需要重启虚拟机
PrintStream psSystem=System.out;
psSystem.println("1234");
System.out.println("666");
}
}
package IOtest16;
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class IODemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File src1 = new File("G:\\JavaProgram\\java\\src\\IOtest16\\aaa.zip");
File dest1 = new File("G:\\JavaProgram\\java\\src\\IOtest16");
unzip(src1, dest1);//执行解压
File src2 = new File("G:\\JavaProgram\\java\\src\\IOtest16\\aaa\\b");
File dest2 = new File("G:\\JavaProgram\\java\\src\\IOtest16", src2.getName() + ".zip");
tozip(src2, dest2);//执行压缩
}
private static void unzip(File src, File dest) throws IOException {
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));//创建解压缩流
ZipEntry entry;//临时接收解压的文件
while ((entry = zip.getNextEntry()) != null) {//获取解压文件
System.out.println(entry);
if (entry.isDirectory()) {//文件夹解压时在目标解压路径创建文件夹
File file = new File(dest, entry.toString());
file.mkdirs();
} else {//文件解压时在目标解压路径拷贝文件
FileOutputStream fos = new FileOutputStream(new File(dest, entry.toString()));
int b;
while ((b = zip.read()) != -1) {
fos.write(b);
}
}
}
zip.close();
}
private static void tozip(File src, File dest) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));//创建压缩流
tozipRun(src,zos, src.getName());//递归部分
zos.close();
}
private static void tozipRun(File src, ZipOutputStream zos, String name) throws IOException {
File[] files = src.listFiles();//获取src中的文件
for (File file : files) {
if (file.isFile()) {
ZipEntry entry=new ZipEntry(name+"\\"+file.getName());//在压缩包中创建同名文件
zos.putNextEntry(entry);
FileInputStream fis=new FileInputStream(file);//拷贝文件内容
int b;
while ((b=fis.read())!=-1){
zos.write(b);
}
fis.close();
} else {//文件夹递归调用tozipRun()
tozipRun(file,zos,name+"\\"+file.getName());
}
}
}
}