IO编程
- 1、流的分类
- 2、字节流
- 2.1 InputStream:字节输入流
- 2.1.1 FileInputStream:文件字节输入流
- 2.2 OutputStream:字节输出流
- 2.2.1 FileOutputStream:文件字节输出流
- 2.3 字节流复制文件
- 3、字节缓冲流
- 3.1 BufferedInputStream:字节输入缓冲流
- 3.2 BufferedOutputStream:字节输出缓冲流,切记要flush
- 4、对象流
- 4.1 ObjectOutputStream:对象序列化流
- 4.2 ObjectInputStream:对象反序列化流
- 4.3 序列化和反序列化注意事项
- 5、字符编码
- 6、字符流
- 6.1 FileReader:文件字符输入流
- 6.2 FileWriter:文件字符输出流
- 6.3 字符流文件复制
- 7、字符缓冲流
- 7.1 BufferedReader:字符输入缓冲流
- 7.2 BufferedWriter:字符输出缓冲流
- 8、打印流
- 9、转换流
- 9.1 InputStreamReader:字节流转化为字符流
- 9.2 OutputStreamWriter:字符流转化为字节流
1、流的分类
2、字节流
- 字节流的父类(抽象类)
2.1 InputStream:字节输入流
2.1.1 FileInputStream:文件字节输入流
- 一次一个字节读取
import java.io.FileInputStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
//创建FileInputStream
FileInputStream fis = new FileInputStream("TTXX.iml");
//一次一个字节读取文件
int data = 0;
while((data = fis.read()) != -1){
System.out.print((char)data/*字节强转字符*/);
}
//关闭流
fis.close();
System.out.println("\n执行完毕");
}
}
- 一次多个字节读取
import java.io.FileInputStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
//创建FileInputStream
FileInputStream fis = new FileInputStream("TTXX.iml");
//一次多个字节读取文件
byte[] buf = new byte[1024];//定义数组,一次读取读取1024个字节,存入数组中
int count = 0;//定义一次读取字节的个数
while((count = fis.read(buf)) != -1){
System.out.print(new String(buf,0,count));
}
//关闭流
fis.close();
System.out.println("\n执行完毕");
}
}
2.2 OutputStream:字节输出流
2.2.1 FileOutputStream:文件字节输出流
import java.io.FileOutputStream;
public class FileOutputStreamDemo {
public static void main(String[] args) throws Exception {
//创建文件字节输出流
FileOutputStream fos = new FileOutputStream("tt.txt",true);//true:表示可追加
//一次一个字节写
fos.write(97);
fos.write('b');
fos.write('c');
String str = "helloworld";
fos.write(str.getBytes());
//关闭流
fos.close();
}
}
2.3 字节流复制文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 使用字节流复制文件
*/
public class CopyFile {
public static void main(String[] args) throws Exception {
//文件字节输入流
FileInputStream fis = new FileInputStream("tt.txt");
//文件字节输出流
FileOutputStream fos = new FileOutputStream("ttt.txt");
//边读边写
byte[] buf = new byte[1024];//缓冲数组,一次读取1024个字节
int count = 0;//记录字节的实际个数
while((count = fis.read(buf)) != -1){
fos.write(buf,0,count);
}
//关闭流
fos.close();
fis.close();
}
}
3、字节缓冲流
- 增加其他流的功能
3.1 BufferedInputStream:字节输入缓冲流
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws Exception{
//创建字节缓冲流
FileInputStream fis = new FileInputStream("tt.txt");//文件输入流每次都是从磁盘中读取数据
//提高读取的效率
BufferedInputStream bis = new BufferedInputStream(fis/*节点流*/);//传参的流应是节点流,每次都是在缓冲区读取数据
// int data = 0;
// while((data = bis.read()) != -1){
// System.out.print((char) data);
// }
byte[] buf = new byte[1024];//创建缓冲数组
int count = 0;
while((count = bis.read(buf)) != -1){
System.out.println(new String(buf,0,count));
}
//关闭流
bis.close();
}
}
3.2 BufferedOutputStream:字节输出缓冲流,切记要flush
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("ttt.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()
}
}
4.1 ObjectOutputStream:对象序列化流
- 学生类
/**
* 学生类
* 实现序列化,学生类需实现Serializable接口,否则会报异常:java.io.NotSerializableException
*/
public class Student implements Serializable {
private static final long serialVersionUID = 100L;
private String name;
private int age;
public Student() {
}
public Student(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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 序列化流
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("tt.txt");
//创建对象流
ObjectOutputStream oos = new ObjectOutputStream(fos/*节点流*/);
//序列化
Student zhangsan = new Student("张三",20);
oos.writeObject(zhangsan);
oos.flush();
//关闭流
oos.close();
}
}
4.2 ObjectInputStream:对象反序列化流
- 学生类
/**
* 学生类
* 实现序列化,学生类需实现Serializable接口,否则会报异常:java.io.NotSerializableException
*/
public class Student implements Serializable {
private static final long serialVersionUID = 100L;
private String name;
private int age;
public Student() {
}
public Student(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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 反序列化流
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("tt.txt");
//创建对象流
ObjectInputStream ois = new ObjectInputStream(fis);
//读取:反序列化
Student s = (Student)ois.readObject();
System.out.println(s);
//关闭流
ois.close();
}
}
4.3 序列化和反序列化注意事项
1)序列化类必须要实现Serializable接口
2)序列化类中对象属性(成员内部类)也要求实现Serializable接口
3)serialVersionUID:序列化版本号ID,保证序列化类和反序列化类是同一个类
4)使用transient(瞬态)修饰属性,这个属性就不能序列化,而序列化后是持久化的存储,所以瞬态的变量就不能序列化
5)静态属性不能序列化
6)序列化多个对象,可以借助集合实现
序列化类
import java.io.Serializable;
/**
* 学生类
* 实现序列化,学生类需实现Serializable接口,否则会报异常:java.io.NotSerializableException
*/
public class Student implements Serializable {
private static final long serialVersionUID = 100L;
private String name;
private transient int age;
public static String country = "中国";
public Student() {
}
public Student(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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 序列化流
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("tt.txt");
//创建对象流
ObjectOutputStream oos = new ObjectOutputStream(fos/*节点流*/);
//序列化
Student zhangsan = new Student("张三",20);
Student lisi = new Student("李四",22);
// oos.writeObject(zhangsan);
// oos.writeObject(lisi);
ArrayList<Student> list = new ArrayList<>();
list.add(zhangsan);
list.add(lisi);
oos.writeObject(list);
oos.flush();
//关闭流
oos.close();
}
}
- 反序列化流
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ObjectInputStreamDemo {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("tt.txt");
//创建对象流
ObjectInputStream ois = new ObjectInputStream(fis);
//读取:反序列化
// Student s = (Student)ois.readObject();
// Student s2 = (Student)ois.readObject();
// System.out.println(s);
// System.out.println(s2);
// System.out.println(s.country);
ArrayList<Student> list = (ArrayList<Student>)ois.readObject();
//关闭流
ois.close();
System.out.println(list.toString());
}
}
6.1 FileReader:文件字符输入流
import java.io.FileReader;
public class FileReaderDemo {
public static void main(String[] args) throws Exception{
//创建文件字符输入流
FileReader fr = new FileReader("tt.txt");
//单个字符读取
// int data = 0;
// while((data = fr.read()) != -1){//如果默认UTF-8,读取一个中文字符,则每次会读3个字节。
// System.out.println((char)data);
// }
//创建字符缓冲区
char[] buf = new char[1024];//字符数组
int count = 0;
while((count = fr.read(buf)) != -1){
System.out.println(new String(buf,0,count));
}
//关闭流
fr.close();
}
}
6.2 FileWriter:文件字符输出流
6.3 字符流文件复制
import java.io.FileReader;
import java.io.FileWriter;
/**
* FileReader和FileWriter复制文本文件,不能复制二进制文件
*/
public class FileReaderWriterDemo {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("tt.txt");
FileWriter fw = new FileWriter("ttt.txt");
int data = 0;
while((data = fr.read()) != -1){
fw.write(data);
fw.flush();
}
//关闭流
fw.close();
fr.close();
}
}
7.1 BufferedReader:字符输入缓冲流
import java.io.BufferedReader;
import java.io.FileReader;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("tt.txt");
//创建缓冲流
BufferedReader br = new BufferedReader(fr);
//读取
// char[] buf = new char[1024];
// int count = 0;
// while((count = br.read(buf)) != -1){
// System.out.println(new String(buf,0,count));
// }
//一行一行读取
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭流
br.close();
}
}
7.2 BufferedWriter:字符输出缓冲流
import java.io.BufferedWriter;
import java.io.FileWriter;
public class BufferedWriterDemo {
public static void main(String[] args) throws Exception{
FileWriter fw = new FileWriter("ttt.txt");
BufferedWriter bw = new BufferedWriter(fw/*传入字符输出流*/);
for (int i = 0; i < 10; i++) {
bw.write("好好学习JAVA");
bw.newLine();//换行符
bw.flush();
}
//关闭流
bw.close();
}
}
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) throws Exception{
//创建打印流
PrintWriter pw = new PrintWriter("tt.txt");
//打印
pw.println(97);
pw.println('a');
pw.println(true);
pw.println(3.14);
//关闭
pw.close();
}
}
9.1 InputStreamReader:字节流转化为字符流
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) throws Exception{
//创建InputStreamReader对象
FileInputStream fis = new FileInputStream("tt.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
//读取文件
int data = 0;
while((data = isr.read()) != -1){
System.out.println((char)data);
}
//关闭流
isr.close();
}
}
9.2 OutputStreamWriter:字符流转化为字节流
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("ttt.txt");
OutputStreamWriter oos = new OutputStreamWriter(fos,"utf-8");
for (int i = 0; i < 10; i++) {
oos.write("java...hello...\r\n");
oos.flush();
}
//关闭流
oos.close();
}
}