IO包中的其他类

序列流:

SequenceInputStream
将多个读取流合并成一个读取流。
练习:切割文件

Ps:匿名内部类访问的变量必须是final 的。

练习:

package it.practise.com;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class SequenceDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		
		v.add(new FileInputStream("G:\\demo\\1.txt"));
		v.add(new FileInputStream("G:\\demo\\2.txt"));
		v.add(new FileInputStream("G:\\demo\\3.txt"));
		
		Enumeration<FileInputStream> en = v.elements();
		
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("G:\\demo\\4.txt");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf)) != -1) {
			bos.write(buf, 0, len);
			bos.flush();
		}
		
		bos.close();
		sis.close();

	}

}


对象的序列化
ObjectOutputStream
构造方法 ObjectOutputStream(OutputStream out) 
重点方法:
void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。


ObjectInputStream
构造方法 ObjectInputStream(InputStream in)
重点方法:
Object readObject() 从 ObjectInputStream 读取对象。
注意:因为如果制定的文件里存储的是其他数据,readObject() 方法会返回不了对象,因此会引起 ClassNotFoundException .

package it.practise.com;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamDemo {

	public static void main(String[] args) {
		try {
			new ObjectStreamDemo().readObj();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void writeObj() throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G:\\demo\\obj.txt"));
		
		oos.writeObject(new Person("lisi", 36));
		
		oos.close();
	}

	public void readObj() throws IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("G:\\demo\\obj.txt"));
		Person p = (Person)ois.readObject();
		System.out.println(p);
		ois.close();
	}
}
class Person implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	
	Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String toString() {
		return name + ":" + age;
	}
}


Ps:(1)想要序列化的对象必须实现 Serializable 接口

    (2)没有方法的借口称为标记借口
    (3)实现 Serializable 接口的类应该自己定义一个serialVersionUID。
    (4)static 和 transient 修饰的属性,不能被序列化。


管道流
PipedInputStream   PipedOutputStream

输入输出可以直接进行连接,通过结合线程使用。

练习:

package it.practise.com;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStreamDemo {

	public static void main(String[] args) throws IOException {
		PipedInputStream in = new PipedInputStream();
		PipedOutputStream out = new PipedOutputStream();
		in.connect(out);
		
		Read r = new Read(in);
		Write w = new Write(out);
		
		new Thread(r).start();
		new Thread(w).start();
	}

}

class Read implements Runnable {
	private PipedInputStream in;
	Read(PipedInputStream in) {
		this.in = in;
	}
	public void run() {
		try {
			byte[] buf = new byte[1024];
			int len = in.read(buf);
			String s = new String(buf, 0, len);
			System.out.println(s);
			in.close();
		} catch(IOException e) {
			throw new RuntimeException("管道读取失败");
		}
	}
	
}
class Write implements Runnable {
	private PipedOutputStream out;
	Write(PipedOutputStream out) {
		this.out = out;
	}
	public void run() {
		try {
			out.write("aaaaaaaaaa".getBytes());
			out.close();
		} catch(IOException e) {
			throw new RuntimeException("管道写出失败");
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值