IO流_5(其他流)

PrintStream

特点

  1. 提供了打印方法print()可以对多种数据类型值进行打印,并保持数据的表现形式,但是不保证数据的大小(即输入与输出的东西看起来一模一样)
  2. 他不抛IOException

SequenceInputStream

特点

序列流
将多个流进行合并

将1.txt、2.txt、3.txt中的数据合并到4.txt中

public class SequenceInputStreamDemo {
	public static void main(String[] args) throws IOException {
		
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		al.add(new FileInputStream("D:\\1.txt"));
		al.add(new FileInputStream("D:\\2.txt"));
		al.add(new FileInputStream("D:\\3.txt"));
	
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("D:\\4.txt");
		
		byte[] b = new byte[1024];
		
		int len = 0;
		
		while((len=sis.read(b)) != -1) {
			fos.write(b,0,len);
		}
		
		sis.close();
		fos.close();
	}
}

文件切割器

public class SplitFile {

	private static final int SIZE = 1024*1024;

	public static void main(String[] args) throws IOException {
		//File file = new File("D:\\IOtest\\1.png");
		File file = new File("E:\\Pictures\\身份证正面.jpg");
		splitFile(file);
	}

	private static void splitFile(File file) throws IOException {
		//用读取流关联源文件
		FileInputStream fis = new FileInputStream(file);
		
		//自定义1M的缓冲区
		byte[] buf = new byte[SIZE];
		
		//创建目的 
		FileOutputStream fos = null;
		
		int len = 0;
		int count = 1;
		File dir = new File("D:\\IOtest\\part");
		if(!dir.exists()) {
			dir.mkdirs();
		}
		while((len = fis.read(buf)) != -1) {
			fos = new FileOutputStream(new File(dir,count++ + ".part"));
			fos.write(buf,0,len);
		}
		
		//保存切割文件的配置信息
		Properties prop = new Properties();
		prop.setProperty("partCount", count+"");
		prop.setProperty("fileName", file.getName());
		prop.store(new FileOutputStream(new File(dir,"my.properties")), "save file info");
		
		fis.close();
		fos.close();
	}
}

合并文件

public class MergeFile {

	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\IOtest\\part");
		mergeFile(dir);
	}

	private static void mergeFile(File dir) throws IOException {
		
		//得到配置信息
		Properties prop = getConfig(dir);
		
		String partCount = (String) prop.get("partCount");
		int count = Integer.parseInt(partCount);
		
		//配置文件校验
		validateConfig(count,dir);

		
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		
		for (int i = 1; i < count; i++) {
			al.add(new FileInputStream(new File(dir,i+".part")));
		}
		
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,prop.getProperty("fileName")));
		
		byte[] buf = new byte[1024];
		
		int len = 0;
		
		while((len = sis.read(buf)) != -1) {
			fos.write(buf, 0, len);
		}
		
		sis.close();
		fos.close();
		
	}

	private static void validateConfig(int count, File dir) {
		String[] list = dir.list(new FilenameFilter() {
			public boolean accept(File arg0, String arg1) {
				boolean endsWith = arg1.endsWith(".part");
				return endsWith;
			}
		});
		if (count - 1 != list.length) {
			throw new RuntimeException("碎片文件个数不对");
		}
	}

	private static Properties getConfig(File dir) throws IOException {
		Properties prop = new Properties();
		
		File file = new File(dir,"my.properties");
		if(!file.exists())
			throw new RuntimeException("配置文件不存在");
		prop.load(new FileInputStream(file));
		
		return prop;
	}
}

Object(input|Output)Stream

一个操作对象的流

代码

public class ObjectOutputStreamDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		
		//writeObj();
		readerObj();
		
	}

	private static void readerObj() throws IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IOtest\\obj.object"));
		Person p = (Person) ois.readObject();
		System.out.println(p.getName()+":"+p.getAge());
	}

	private static void writeObj() throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IOtest\\obj.object"));
		oos.writeObject(new Person("小强",18));
		oos.close();
	}
}

对象序列化

Serializable:

  • 用于给被序列化的类加入ID号
  • 用于判断类和对象是否是同一个版本
public class Person implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	
	
	public Person(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;
	}
}

RandomAccessFile

特点

  1. 该对象既能读,也能写
  2. 该对象内部维护了一个byte数组,并通过指针可以操作数组中的元素
  3. 可以通过getFilePointer方法获取指针的位置,和通过seek方法设置指针的位置
  4. 其实该对象就是讲字节输入流和输出流进行了封装
  5. 该对象的源和目的只能是文件。

代码

public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		/*
		 * 特点:
		 * 1.该对象既能读,也能写
		 * 2.该对象内部维护了一个byte数组,并通过指针可以操作数组中的元素
		 * 3.可以通过getFilePointer方法获取指针的位置,和通过seek方法设置指针的位置
		 * 4.其实该对象就是讲字节输入流和输出流进行了封装
		 * 5.该对象的源和目的只能是文件。
		 */
		
		writerFile();
		readerFile();
	}
	
	private static void readerFile() throws IOException {
		RandomAccessFile raf = new RandomAccessFile("D:\\IOtest\\random.txt", "r");
		
		//通过seek设置指针的位置
		raf.seek(0*8);//随机的读取。只要指定指针的位置即可
		
		byte[] buf = new byte[4];
		raf.read(buf);
		
		String name = new String(buf);
		
		int age = raf.readInt();
		System.out.println("name="+name);
		System.out.println("age="+age);
		
		System.out.println("pos:"+raf.getFilePointer());
		
		raf.close();
	}

	//使用RandomAccessFile对象写入一些人员信息,比如姓名和年龄
	private static void writerFile() throws IOException {
		RandomAccessFile raf = new RandomAccessFile("D:\\IOtest\\random.txt", "rw");
		
		raf.write("张三".getBytes());
		raf.writeInt(97);
		
		raf.write("李四".getBytes());
		raf.writeInt(32);
		
		raf.close();
	}
}

管道流

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

public class PipedStreamDemo {

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


class Input implements Runnable{
	PipedInputStream in;
	
	Input(PipedInputStream in){
		this.in = in;
	}
	@Override
	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);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class Output implements Runnable{
	PipedOutputStream out;
	
	Output(PipedOutputStream out){
		this.out = out;
	}
	@Override
	public void run() {
		try {
			Thread.sleep(5000);
			out.write("管道来了!".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

DataStream

操作基本数据类型用该流

public class DataStreamDemo {
	public static void main(String[] args) throws IOException {
		writeDate();
		readDate();
	}

	@SuppressWarnings("resource")
	private static void readDate() throws IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream("D:\\IOtest\\data.txt"));
		String s = dis.readUTF();
		System.out.println(s);
		dis.close();
	}

	@SuppressWarnings("resource")
	private static void writeDate() throws IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\\IOtest\\data.txt"));
		dos.writeUTF("你好");
		dos.close();
	}
}

猜你喜欢

转载自blog.csdn.net/cw13223/article/details/84996256