JAVA : IO框架

流的概念:内存与存储设备之间传输数据的通道。数据借助流传输。
流的分类:

按方向:
输入流:将<存储设备>(硬盘)中的内容读入到<内存>中。
输出流:将<内存>中的内容写入到<存储设备>中。

按单位:
字节流:以字节为单位,可以读写所有数据 。
	InputStream : 字节输入流
	FileInputStream:字节节点流
	Reader : 字符输入流
	BufferedInputStream : 字节缓冲流
	ObjectInputStream : 对象流
	FileReader:字符节点流
字符流:以字符为单位,只能读写文本数据 。
	OutputStream : 字节输出流
	FileOutputStream:字节节点流
	Writer :  字符输出流
	BufferedOutputStream :字节缓冲流
	ObjectOutputStream : 对象流
	FileWriter : 字符节点流
按功能:
节点流:具有实际传输数据的读写功能。
过滤流:在节点流的基础之上增强功能。

字节节点流:
FileOutputStream : 一次写多个字节,将b数组中所有字节,写入输出流。
FileInputStream : 从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数。如果达到文件的尾部,则返回-1。

public class TestFileInputStream {
	public static void main(String[] args) throws Exception{
		//1创建FileInputStream,并指定文件路径
		FileInputStream fis=new FileInputStream("d:\\aaa.txt");
		//2读取文件
		//fis.read()
		//2.1单个字节读取
		//int data=0;
		//while((data=fis.read())!=-1) {
		//	System.out.print((char)data);
		//}
		//2.2一次读取多个字节
		byte[] buf=new byte[1024];
		int count=0;
		while((count=fis.read(buf))!=-1) {
			System.out.println(new String(buf,0,count));
		}
		
		//3关闭
		fis.close();
		System.out.println();
		System.out.println("执行完毕");
	}
}

public class TestFileOutputStream {
	public static void main(String[] args) throws Exception{
		//1创建文件字节输出流对象
		FileOutputStream fos=new FileOutputStream("d:\\bbb.txt",true);
		//2写入文件
		//fos.write(97);
		//fos.write('b');
		//fos.write('c');
		String string="helloworld";
		fos.write(string.getBytes());
		//3关闭
		fos.close();
		System.out.println("执行完毕");
	}
}
//使用字节流存储数据
//2一边读,一边写
		byte[] buf=new byte[1024];
		int count=0;
		while((count=fis.read(buf))!=-1) {
			fos.write(buf,0,count);
		}
		//3关闭
		fis.close();
		fos.close();
		System.out.println("复制完毕");
字节缓冲流:

提高IO效率,减少访问磁盘的次数。数据存储在缓冲区中,flush是将缓存区的内容写入文件中,也可以直接close。

OutputStream os = new FileOutputStream();
os.write(要写的数据);
os.flush();
os.close();
对象流:
增强了缓冲区功能。增强了读写8种基本数据类型和字符串功能。增强了读写对
象的功能。
eadObject() 从流中读取一个对象。 writeObject(Object obj)
使用流传输对象的过程称为序列化、反序列化。

序列化的细节:
必须实现Serializable接口。
必须保证其所有属性均可序列化。
transient修饰为临时属性,不参与序列化。
读取到文件尾部的标志:java.io.EOFException。
//对象了实现序列化反序列化
public class Student implements Serializable{

	/**
	 * serialVersionUID:序列化版本号ID,
	 */
	private static final long serialVersionUID = 100L;
	private String name;
	private transient int age;
	
	public static String country="中国";
	
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		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 + "]";
	}
}

public class TestSerializable {
	public static void main(String[] args) throws Exception{
		//1创建对象流
		FileOutputStream fos=new FileOutputStream("d:\\stu.bin");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		//2序列化(写入操作)
		Student zhangsan=new Student("张三", 20);
		Student lisi=new Student("李四", 22);
		ArrayList<Student> list=new ArrayList<>();
		list.add(zhangsan);
		list.add(lisi);
		oos.writeObject(list);
	
		//3关闭
		oos.close();
		System.out.println("序列化完毕");
	}
}

public class TestDeSerializable  {
	public static void main(String[] args) throws Exception {
		//1创建对象流
		FileInputStream fis=new FileInputStream("d:\\stu.bin");
		ObjectInputStream ois=new ObjectInputStream(fis);
		//2读取文件(反序列化)
		//Student s=(Student)ois.readObject();
		//Student s2=(Student)ois.readObject();
		ArrayList<Student> list=(ArrayList<Student>)ois.readObject();
		//3关闭
		ois.close();
		System.out.println("执行完毕");
		//System.out.println(s.toString());
		//System.out.println(s2.toString());
		System.out.println(list.toString());
	}
}

桥转换流:可以将字节流转换成字符流。可设置字符的编码方式。

//1创建OutputStreamWriter
		FileOutputStream fos=new 
			FileOutputStream("d:\\info.txt");
		OutputStreamWriter osw=new 
			OutputStreamWriter(fos, "utf-8");

Properties实现流操作:

特点:

  • 存储属性名和属性值。
  • 属性名和属性值都是字符串类型。
  • 没有泛型。
  • 和流有关。
public class TestProperties {
	public static void main(String[] args) throws Exception {
		//1创建集合
		Properties properties=new Properties();
		//2添加数据
		properties.setProperty("username", "zhangsan");
		properties.setProperty("age", "20");
		System.out.println(properties.toString());
		//3遍历
		//3.1-----keySet----略
		//3.2-----entrySet----略
		//3.3-----stringPropertyNames()---
		Set<String> pronames=properties.stringPropertyNames();
		for (String pro : pronames) {
			System.out.println(pro+"====="+properties.getProperty(pro));
		}
		//4和流有关的方法
		//----------1、list方法---------
		PrintWriter pw=new PrintWriter("d:\\print.txt");
		properties.list(pw);
		pw.close();
		
		//----------2、store方法 保存-----------
		FileOutputStream fos=new FileOutputStream("d:\\store.properties");
		properties.store(fos, "注释");
		fos.close();
		
		//----------3、load方法 加载-------------
		Properties properties2=new Properties();
		FileInputStream fis=new FileInputStream("d:\\store.properties");
		properties2.load(fis);
		fis.close();
		System.out.println(properties2.toString());
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值