字节流扩展实例
FileInputStream
import java.io.FileInputStream;
/**
* @author Nigori
* @date 2020/8/8
**/
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("IOStreamTest/FileInputStreamDemo.txt");
byte[] bytes = new byte[5];
int count = 0;
while ((count=fis.read(bytes))!=-1) {
System.out.println(new String(bytes,0,count));
}
fis.close();
}
}
FileOutputStream
import java.io.FileOutputStream;
/**
* @author Nigori
* @date 2020/8/8
**/
public class FileOutputStreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("IOStreamTest/FileOutputStreamDemo.txt");
for (int i = 0; i < 10; i++) {
fos.write("Java EE框架整合开发!\r\n".getBytes());
}
fos.close();
}
}
ObjectInputStream
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
/**
* 使用ObjectInputStream属性反序列化(读取重构成对象)
* @author Nigori
* @date 2020/8/8
**/
public class DeserializeDemo {
public static void main(String[] args) throws Exception {
//创建流对象
FileInputStream fis = new FileInputStream("IOStreamTest/serializedemo.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
//读取文件(反序列化)
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
//关闭流
ois.close();
System.out.println(list.toString());
}
}
ObjectOutputStream
使用ObjectOutputStream实现对象的序列化
- 注意事项:
- (1)序列化类必须实现Serializable接口
- (2)序列化类中的对象属性必须实现Serializable接口【例:private Teacher teacher;时,Teacher要实现接口】
- (3)序列化版本ID,保证序列化的类和反序列化的类是同一个类
- (4)使用transient(瞬间的)修饰的属性不能序列化【例:private transient int num;】
- (5)静态属性不能序列化
- (6)序列化多个对象借助集合实现
public class Student implements Serializable { ... }
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
* 使用ObjectOutputStream实现对象的序列化
* @author Nigori
* @date 2020/8/8
**/
public class SerializeDemo {
public static void main(String[] args) throws Exception {
//创建对象流
FileOutputStream fos = new FileOutputStream("IOStreamTest/serializedemo.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//序列化(写入)
Student student_1 = new Student("小明",26);
Student student_2 = new Student("小红",25);
Student student_3 = new Student("小白",23);
ArrayList<Student> list = new ArrayList<>();
list.add(student_1);
list.add(student_2);
list.add(student_3);
oos.writeObject(list); //java.io.NotSerializableException(未实现Serializable接口)
oos.close();
}
}
BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
* 使用BufferedInputStream(字节缓冲输入流)读到内存
* @author Nigori
* @date 2020/8/8
**/
public class BufInStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("IOStreamTest/BufInStreamDemo.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
/*//单个字节读取
int data = 0;
while ((data=bis.read())!=-1) {
System.out.println((char) data);
}*/
//多字符读取
byte[] bytes = new byte[1024];
int count = 0;
while ((count=bis.read(bytes))!=-1) {
System.out.println(new String(bytes,0,count));
}
//关闭
bis.close();
}
}
BufferedOutputStream
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
/**
* 使用BufferedOutputStream(字节缓冲流输出流)写入文件
* @author Nigori
* @date 2020/8/8
**/
public class BufOutStreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("IOStreamTest/BufOutStreamDemo.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
for (int i = 0; i < 10; i++) {
bos.write("helloworld\r\n".getBytes()); //写入8K缓冲区
bos.flush(); //刷新到硬盘
}
bos.close(); //内部调用flush();
}
}