Java基础——I/O流基础

本文介绍了Java中流的概念,包括字节流、字符流、节点流和过滤流的应用,详细讲解了FileInputStream/FileOutputStream、ByteArrayInputStream/ByteArrayOutputStream以及ObjectInputStream/ObjectOutputStream的使用。通过实例演示了如何复制文件、内存数据传输和对象序列化的过程。

作用

传输数据

分类

按流向分类
	输入流:外界给程序中输入
	输出流:从程序给外界输入数据
按传输的最小数据单位:
	字节流:传输的数据单位是字节
	字符流:传输的数据单位是字符,只能传递文本文件
按功能分类:
	节点流:从源头到程序
	过滤流(包装流):加工
		都有其特性

字节流

体系结构:
	InputStream(抽象类)
		方法:
			read()
			read(byte[] b);
			read(byte[] b, int off, int len)
			
			close()
	OutputStream(抽象类)
		方法:
			write(byte b)
			write(byte[] b)
			write(byte[] b, int off, int len)
			
			close()
			flush():冲刷管道,防止残留

文件字节流

FileInputStream:从文件到程序
FileOutputStream:从程序到文件
练习:从一个文件夹赋值照片到另一个文件夹
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件的复制
 */
public class Demo03 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("C://Users/豆三岁/Desktop/壁纸.jpg");
			fos = new FileOutputStream("F://照片/壁纸.jpg");
			//读取数据
			byte[] b = new byte[1024];
			int len = 0;
			//循环读取数据
			while ((len = fis.read(b)) != -1) {
				//输出流输出
				fos.write(b,0,len);
				fos.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

内存字节流

ByteArrayInputStream:从内存到程序
ByteArrayOutputStream:从程序到内存
练习:将小说.txt文本文件,输出到控制台
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo05 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null;
		try {
			fis = new FileInputStream("src/小说.txt");
			bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int len = 0;
			while((len = fis.read(b)) != -1) {
				bos.write(b, 0, len);
				bos.flush();
			}
			byte[] data = bos.toByteArray();
			String str = new String(data);
			System.out.println(str);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

缓冲字节流(过滤流)

BufferedInputStream
BufferedOutputStream
作用:加快速度
练习:利用缓冲字节流复制文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 当数据量少得时候看不出其优点
 * 当数据量足够大的时候,就可以提现其优点了
 */
public class Demo06 {
	public static void main(String[] args) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("src/小说.txt");
			bis = new BufferedInputStream(fis);
			
			fos = new FileOutputStream("copy.txt");
			bos = new BufferedOutputStream(fos);
			
			byte[] b = new byte[2048];
			int len = 0;
			
			while((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
				bos.flush();
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (bos != null) {
				/**
				 * 过滤流在关闭时,会关闭他所包装的节点流
				 */
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bis != null) {
				/**
				 * 过滤流在关闭时,会关闭他所包装的节点流
				 */
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

对象流(过滤流)

ObjectInputStream
	readObject();
ObjectOutputStream
	writeObject(Object obj);
作用:给指定存入对象
注意:
	1、存储的对象要实现序列化接口
	2、实现序列化接口的类中有static修饰的属性,该属性无法被序列化
	3、transient修饰的属性也不能被序列化(transient修饰的属性称为临时属性)
练习:利用对象流存储数据
//实体类
import java.io.Serializable;

public class Person implements Serializable{
	private String name;
	private int age;
	private transient String sex;
	private static String gj;
	public Person() {
		super();
	}
	public Person(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		Person.gj = "中国"; 
	}
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public static String getGj() {
		return gj;
	}
	public static void setGj(String gj) {
		Person.gj = gj;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex +", gj="+ gj + "]";
	}
}
//测试类01
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Demo07 {
	public static void main(String[] args) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try {
			fos = new FileOutputStream("object.txt");
			oos = new ObjectOutputStream(fos);
			Person person = new Person("高磊", 16, "男");
			Person person02 = new Person("姬瑶葳", 98, "男");
//			oos.writeObject(person);
//			oos.writeObject(person02);
			ArrayList<Person> list = new ArrayList<Person>();
			list.add(person);
			list.add(person02);
			oos.writeObject(list);
			oos.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		System.out.println("OVER");
	}
}
//测试类02
package com.qf.demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Demo08 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		
		try {
			fis = new FileInputStream("object.txt");
			ois = new ObjectInputStream(fis);
			
			ArrayList<Person> persons = (ArrayList<Person>) ois.readObject();
			for (Person person : persons) {
				System.out.println(person);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值