缓冲流
缓冲流也分为四种流,分别对应字节和字符的输入输出流,在前面加上Buffered
底层自带了长度为8192的缓冲区提高性能
字节缓冲流
单字节
java
import java.io.*;
public class BufferedStreamDemo1 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("rubish\\src\\file\\b.txt"));
int b;
while ((b = bis.read()) != -1){
bos.write(b);
}
bos.close();
bis.close();
}
}
多字节
java
import java.io.*;
public class BufferedStreamDemo2 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("rubish\\src\\file\\b.txt"));
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
}
字符缓冲流
字符缓冲流提升的效率并不明显
字符缓冲输入流
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedStreamDemo3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("rubish\\src\\file\\a.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
字符缓冲输出流
java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedStreamDemo4 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("rubish\\src\\file\\a.txt"));
bw.write("hello");
bw.newLine();//字符特有功能
bw.write("world");
bw.newLine();
bw.close();
}
}
转换流
是字符流和字节流的桥梁
利用转换流按照指定的字符编码读取,替代方法
java
import java.io.*;
import java.nio.charset.Charset;
public class ConvertStreamDemo1 {
public static void main(String[] args) throws IOException {
/*
利用转换流按照指定的字符编码读取,替代方法
*/
/* InputStreamReader isr = new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt"),"UTF-8");
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char)ch);
}
isr.close();*/
FileReader fr = new FileReader("rubish\\src\\file\\a.txt", Charset.forName("UTF-8"));
int ch;
while((ch = fr.read())!=-1){
System.out.print((char)ch);
}
fr.close();
}
}
利用转换流按照指定字符编码写出
java
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
public class ConvertStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
利用转换流按照指定字符编码写出
*/
/* OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("rubish\\src\\file\\a.txt"),"UTF-8");
osw.write("rubish");
osw.close();*/
FileWriter fw = new FileWriter("rubish\\src\\file\\a.txt", Charset.forName("UTF-8"));
fw.write("Hello World");
fw.close();
}
}
将本地文件中的GBK文件转成UTF-8
java
import java.io.*;
import java.nio.charset.Charset;
public class ConvertStreamDemo3 {
public static void main(String[] args) throws IOException {
/*
将本地文件中的GBK文件转成UTF-8
*/
// jdk11以前的方案
/* InputStreamReader isr = new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("rubish\\src\\file\\b.txt"),"UTF-8");
int b;
while((b=isr.read())!=-1){
osw.write(b);
}
osw.close();
isr.close();*/
FileReader fr = new FileReader("rubish\\src\\file\\a.txt", Charset.forName("GBK"));
FileWriter fw = new FileWriter("rubish\\src\\file\\b.txt", Charset.forName("UTF-8"));
int b;
while((b = fr.read())!=-1){
fw.write(b);
}
fw.close();
fr.close();
}
}
利用字节流读取文章,且不出现乱码
java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class ConvertStreamDemo4 {
/*
利用字节流读取文章,且不出现乱码
*/
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("rubish\\src\\file\\a.txt")));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
注:注释的代码为jdk11之前书写的方式
序列化流与反序列化流
java
import java.io.Serializable;
public class Teacher implements Serializable {
/*
Serializable接口里面是没有抽象方法,标记型接口
一旦实现了这个接口,那么就表示当前的Student类可以序列化
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
//transient 瞬态关键字
// 作用:不会把当前属性序列化到本地文件当中
public Teacher(){}
public Teacher(String name,int age){
this.name=name;
this.age=age;
}
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 toString(){
return "Student{name = " + name + ", age =" + age + "}";
}
}
序列化
java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamDemo1 {
public static void main(String[] args) throws Exception {
Teacher Teacher = new Teacher("Zhangsan",22);
// 使用对象输出流将对象保存到文件时会出现NoSerializableException
// 解决方法:需要让javabean类实现Serializable接口
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("rubish\\src\\file\\a.txt"));
oos.writeObject(Teacher);
oos.close();
}
}
反序列化
java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
public class ObjectStreamDemo2 {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("rubish\\src\\file\\a.txt"));
Object o = ois.readObject();
System.out.println(o);
ois.close();
}
}
打印流
打印流只有两种,字符打印流,字节打印流
字符打印流
java
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
public class PrintStreamDemo1 {
public static void main(String[] args) throws Exception{
// 创建字节打印流的对象
PrintStream ps = new PrintStream(new FileOutputStream("rubish\\src\\file\\a.txt"),true, Charset.forName("UTF-8"));
ps.println(97);
ps.print(true);
ps.println();
ps.printf("%s 爱上了 %s","阿珍","阿强");
ps.close();
}
}
字节打印流
java
import java.io.FileWriter;
import java.io.PrintWriter;
public class PrintStreamDemo2 {
public static void main(String[] args) throws Exception{
PrintWriter pw = new PrintWriter(new FileWriter("rubish\\src\\file\\a.txt"),true);
pw.println("1234567890");
pw.print("hello");
pw.printf("%s 爱上了 %s","阿珍","阿强");
pw.close();
}
}
解压缩流和压缩流
解压缩文件
java
{
public static void main(String[] args) throws IOException {
File src = new File("rubish\\src\\file\\c.zip");
File dest = new File("rubish\\src\\file");
unzip(src,dest);
}
public 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()) {
// 文件夹:需要在目的地dest除创建一个同样的文件夹
File file = new File(dest, entry.toString());
file.mkdirs();
}else {
// 文件:需要读取到压缩包中的文件,并把它存放到目的地dest文件夹中
FileOutputStream fos = new FileOutputStream(new File(dest, entry.toString()));
int b;
while ((b = zip.read()) != -1) {
// 写到目的地
fos.write(b);
}
fos.close();
zip.closeEntry();
}
}
zip.close();
}
}
压缩流
压缩文件
java
import java.io.File;
public static class ZipStreamDemo2 {
// 压缩单个文件
public static void main(String[] args) throws IOException {
File src = new File("rubish\\src\\file\\a.txt");
File dest = new File("rubish\\src\\file");
toZip(src,dest);
}
}
public static void toZip(File src,File dest) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
ZipEntry entry = new ZipEntry("a.txt");
zos.putNextEntry(entry);
FileInputStream fis = new FileInputStream(src);
int b;
while ((b = fis.read()) != -1) {
zos.write(b);
}
zos.closeEntry();
zos.close();
}
压缩文件夹
java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipStreamDemo3 {
// 压缩文件夹
public static void main(String[] args) throws IOException {
File src = new File("rubish\\src\\file");
// 创建压缩包父级路径
File destParent = src.getParentFile();
File dest = new File(destParent,src.getName()+".zip");
// 创建压缩流关联压缩包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
zos.close();
}
public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {
File[] files = src.listFiles();
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();
zos.closeEntry();
}else {
toZip(file,zos,name+"\\"+file.getName());
}
}
}
}