天天看点

Lesson_for_java_day18--java中的IO流(序列化、ByteArrayStream、DataStream、RandowAccessFile)

一、序列化:

package sonyi;

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

public class SerializeDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		write();//写
		read();//读
	}
	
	//反序列化
	public static void read(){
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("obj.txt"));
			ArrayList<Person> arr = (ArrayList<Person>)ois.readObject();
			for(Person p:arr){//按顺序读取对象
				System.out.println(p);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(ois != null)
					ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//序列化
	public static void write(){
		ObjectOutputStream oos = null;
		ArrayList<Person> arr = new ArrayList<Person>();//创建容器装对象
		try {
			oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
			arr.add(new Person("zhangsan", 25));
			arr.add(new Person("lisi", 26));
			arr.add(new Person("wangwu", 27));
			
			oos.writeObject(arr);//将容器作为对象传递给流	
			//System.out.println(oos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(oos != null)
					oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

class Person implements Serializable{
	private String name;
	private int age;
	Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", 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;
	}
}
           

二、ByteArrayStream

package io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
	用于操作字节数组的流对象:
		ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组
		ByteArrayOutputStream:在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组
			这就是数据目的地。
		因为这两个流对象都操作的数组,并没有使用系统资源,所以不用进行close关闭。
		
		在流操作的讲解时:
			源设备:键盘(System.in)、硬盘(FileStream)、内存(ArrayStream)
			目的设备:控制台(System.out)、硬盘(FileStream)、内存(ArrayStream)
			
		用流的读写思想来操作数组。
 */
public class ByteArrayStream {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//数据源
		ByteArrayInputStream bis = new ByteArrayInputStream("abdcefg".getBytes());
		
		//数据目的
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int by = 0;
		while((by = bis.read()) != -1){
			bos.write(by);
		}
		System.out.println(bos.size());
		System.out.println(bos.toString());
		
		//bos.writeTo(new FileOutputStream("a.txt"));
	}
}
           

三、DataStream

package io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
	基本数据类型流对象:
		DataInputStream与DataOutputStream:
			可以用于操作基本数据类型的数据的流对象
 */
public class DataStreamDemo {
	public static void main(String[] args) {
//		writeData();
//		readData();
		 writeUTFDemo();
		 readUTFDemo();
	}
	
	public static void readUTFDemo(){
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new FileInputStream("utfdata.txt"));
			String s = dis.readUTF();
			System.out.println(s);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(dis != null)
					dis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void writeUTFDemo(){
		DataOutputStream dos = null;
		try {
			dos = new DataOutputStream(new FileOutputStream("utfdata.txt"));
			dos.writeUTF("你好");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(dos != null)
					dos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void readData(){
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new FileInputStream("data.txt"));
			int num = dis.readInt();
			boolean b = dis.readBoolean();
			double d = dis.readDouble();
			
			System.out.println("num = " + num);
			System.out.println("b = " + b);
			System.out.println("d = " + d);			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(dis != null)
					dis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public static void writeData(){
		DataOutputStream dos = null;
		try {
			dos = new DataOutputStream(new FileOutputStream("data.txt"));
			dos.writeInt(124);
			dos.writeBoolean(true);
			dos.writeDouble(9887.123);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(dos != null)
					dos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
           

四、RandowAccessFile

package io;

import java.io.RandomAccessFile;
/*
	RandowAccessFile:(可以实现多线程对文件的同时操作。比如下载)随机访问文件
		该类不算是IO体系中子类,而是直接继承自Object。
		但是它是IO包中成员,因为它具备读和写功能,内部封装了一个数组,
		而且通过指针对数组的元素进行操作,可以通过getFilePointer获取指针位置。
		同时可以通过seek改变指针的位置。
	
		其实完成读写的原理就是内部封装了字节输入流和输出流。
		通过构造函数可以看出,该类自能操作文件。
		而且操作文件还有模式:
			只读r,读写rw等。
			如果模式为只读,不会创建文件,会去读取一个已存在的文件,如果该文件不存在,则会出现异常
			如果模式为读写,那么该对象的构造函数要操作的文件如果不存在,会自动创建,如果存在,不会覆盖
	
 */
public class RandowAccessFileDemo {
	public static void main(String[] args){
		try {
			//writeFile();
			//readFile();
			writeFile_2();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void readFile() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("ran.txt", "r");
		//调整对象中指针,可以随意设置
		//raf.seek(8);//访问第二个对象了
		
		//跳过指定的字节数
		raf.skipBytes(8);//只能往后调,不能往回调
		byte[] buf = new byte[4];
		raf.read(buf);
		String name = new String(buf);
		int age = raf.readInt();
		System.out.println("name = " + name + ",age = " + age);
		raf.close();
		
	}
	
	public static void writeFile_2() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");
		//raf.seek(8*3);//跳到指定位置
		raf.write("周期".getBytes());
		raf.writeInt(103);
		raf.close();
	}
	
	public static void writeFile() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");
		raf.write("李四".getBytes());
		raf.writeInt(97);
		raf.write("王五".getBytes());
		raf.writeInt(98);
		raf.close();
	}
}