目录
ObjectOutputStream序列化对象(从缓存持久化到硬盘)
ObjectInputStream对象反序列化(从硬盘读取到缓存)
案例,使用BufferedReader BufferedWriter完成从一个hh.txt文本文件中一次读取一行,并判断这一行中有指定字符串,就把这一行数据写入zz.txt文件中
使用File类方法递归遍历文件夹获取所以文件
public static void main(String[] args) {
//递归遍历文件夹获取所以文件
File file = new File("D:\\dev");
getAllFile(file);
}
private static void getAllFile(File file) {
File[] list = file.listFiles();
for (File url:list ) {
if (url.isDirectory()){
getAllFile(url);
}else {
System.out.println(url);
}
}
}
缓冲流
一、概述
缓冲流也叫高效流,都是在基本的流对象基础之上创建而来的,就像穿上铠甲的武士一样,是对4个基本的FileXXXX流的增强,所以也是4个流,按照数据类型分类,
1.字节缓冲流:BufferedInputStream,BufferedOutputStream
2.字符缓冲流:BufferedReader,BufferedWriter
1.BufferedInputStream缓冲字节输入流
public class Demo002BUffered {
public static void main(String[] args) throws IOException {
//创建一个缓冲输入流,参数是文件输入流(即从硬盘往缓存中读取数据)
BufferedInputStream bis = new BufferedInputStream( new FileInputStream("003-Properties\\a.txt"););
int len = 0;
//read()读取一个字节并指向下一个字节,如果达到流的末尾, 返回-1
while ((len = bis.read()) != -1){
System.out.println(len);
}
bis.close();
}
}
2.BufferedOutputStream缓冲字节输出流
案例,从aa文件夹复制图片到cc文件夹
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos =null;
try {
//创建一个整数int来接收每次读取的字节数
int leg = 0;
//创建一个字节数组,每次读取数组的大小字节
byte[] bytes = new byte[1024];
//创建一个缓冲输入流读取图片内容
bis = new BufferedInputStream(new FileInputStream("D:\\aa\\a5.jpeg"));
//判断D:\cc文件件是否存在,不存在创建
File file = new File("D:\\cc\\");
if(!file.exists()){
file.mkdir();
}
//创建缓冲输出流,把图片信息输出到D:\cc\a5.jpeg
bos = new BufferedOutputStream(new FileOutputStream("D:\\cc\\a6.jpg"));
//开始从D:\cc\a5.jpeg读取图片信息
while ((leg = bis.read(bytes)) != -1){
//write(bytes)每次读取1024字节,就写1024字节
bos.write(bytes);
bos.flush();
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节流可以读取字符,图片,流媒体,但是读取汉字就是乱码字符,所以就用到了字符流
3.BufferedReader
public static void main(String[] args) throws IOException {
//缓冲字符输入流
int leg = 0;
BufferedReader br = new BufferedReader(new FileReader("003-Properties\\a.txt"));
while ((leg = br.read()) != -1){
System.out.println(leg);
}
}
4.BufferedWriter
public static void main(String[] args) throws IOException {
//trur在原来的文件中追加
BufferedWriter bw = new BufferedWriter(new FileWriter("003-Properties\\a.txt",true));
for (int i = 0; i < 10; i++) {
bw.write("赵奕欢");
bw.newLine();//换行符
bw.flush();
}
bw.close();
}
----------------------------------------------------------------
字符转换流
InputStreamReader字符转换输入流
OutputStreamWriter字符转换输出流
案例,
/**
* 将GBK编码的文件,转换为UTF-8文本文件
*/
public class Demo001 {
public static void main(String[] args) throws IOException {
//记录每次读的字符数
int len = 0;
//每次读取1024个字符
char[] chars = new char[1024];
//字符输入流:参数1文件输入流,参数2以什么编码读取文件,不写默认UTF-8
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\aa\\a.txt"),"GBK");
//字符输出流:参数1文件输出流,参数2以什么编码存储文件,不写默认UTF-8
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\cc\\b.txt"));
while ((len = isr.read(chars)) != -1){
osw.write(chars);
}
osw.flush();
osw.close();
isr.close();
}
}
对象序列化
概述
java提供了一种对象序列化的机制,用一个字节序列可以表示一个对象,该字节序列化包含该对象的数据、对象的类型、对象中存储的属性等信息,字节序列写出到文件之后,相当于文件中持久了一个对象的信息。
反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化,对象的数据、对象的类型、对象中存储的数据信息,都可以用来在内存中创建对象。
ObjectOutputStream序列化对象(从缓存持久化到硬盘)
public class Demo001objectOutputStream {
public static void main(String[] args) throws IOException {
//创建ObjectOutputStream对象,构造方法中传递字节输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\cc\\c.txt"));
oos.writeObject(new Student("小腿","男",22));
oos.flush();
oos.close();
}
}
ObjectInputStream对象反序列化(从硬盘读取到缓存)
public class ObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
java.io.ObjectInputStream ois = new java.io.ObjectInputStream(new FileInputStream("D:\\cc\\c.txt"));
Object stu = ois.readObject();
System.out.println(stu);
}
}
transient关键字:瞬态关键字
被transient关键字修饰的成员变量,不能被序列化
简单案例序列化对象集合和反序列化对象集合
public static void main(String[] args) throws IOException, ClassNotFoundException {
//练习,序列化对象集合
ArrayList al = new ArrayList();
al.add(new Student("赵倩","女",20));
al.add(new Student("王五","女",21));
al.add(new Student("袁华","男",22));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\cc\\d.txt"));
oos.writeObject(al);
//反序列化对象集合
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\cc\\d.txt"));
ArrayList<Student> i =(ArrayList<Student>) ois.readObject();
for(Student n:i){
System.out.println(n);
}
}
案例,使用BufferedReader BufferedWriter完成从一个hh.txt文本文件中一次读取一行,并判断这一行中有指定字符串,就把这一行数据写入zz.txt文件中
package com.srz.io;
import java.io.*;
/**
* 读取txt文件
* 一次读一行
* @author ylq
*
*/
public class TestIO {
public static void main(String[] args) {
//文件路径
String filePath="D:/hh.txt";
File file=new File(filePath);
BufferedReader reader = null;//字符缓冲输入流进行读取操作读取
BufferedWriter writer = null;//字符缓冲输出流
String tempString = null;//每一行的内容
try {
//输入字节流,FileInputStream主要用来操作文件输入流
FileInputStream intput = new FileInputStream(file);
//字节输出流
FileOutputStream outPut = new FileOutputStream("D:\\zz.txt");
// System.out.println("以行为单位读取文件内容,一次读一整行:")
//InputStreamReader是转换流,将字节流转成字符流
reader = new BufferedReader(new InputStreamReader(intput));
writer = new BufferedWriter(new OutputStreamWriter(outPut));
while ((tempString = reader.readLine()) != null) {
//判断该行是否包含指定字符串
if (tempString.indexOf("abc") != -1){
//包含就写入zz.txt
writer.write(tempString);
writer.newLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}