黑马程序员_java入门_IO流_6(对象序列号)

------- android培训java培训、期待与您交流! ----------

一.对象序列化

所谓的对象序列化就是把对象进行流化。说白了就是通过流管道传对象

 

 

步骤:1.建立一个对象类

class Person implements Serializable//要实现序列化先要实现Serializable接口,或者Externalizable接口

{

      static final long serialVersionUID = 42L;//自定义serialVersionUID

      String name;

     transient int age;//transient修饰成员不能被序列化

     static String country country=”cn“;//静态成员也不能被序列化

     Person(String name,int age,String country) 

{

            this.name=name;

            this.age=age;

            this.country=country;

}

 

}

步骤二 。在一个objectOutputStream流里面写对象

步骤三,在ObjectInoutStream流里面读出对象

class ObjectStreamDemo
{
	public static void main(String[] args)
	{
		


	}
	public stativ void readObject()
	{
		ObjectInputStream ois=new ObjectInputStream(new FileOutputstream("obj.txt"));
		Person p=(Person)ois.readObject();
		sop(p);
		ois.close();

	}
	public static void writeObject() throws IOException
	{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputstream("obj.txt"));
		oos.writeobject(new Person("lisi",39));
		oos.close();

	}




}
class Person implemnts Serializable
{
	String name;
	int age;
	Person(String name,int age)
	{
		this.age=age;
		this.name=name;
	}
	public String toString()
	{
		return name+":"age;
	}

}


 

 

二。管道流

输入输出可以直接连接PipedInputStream和PipedOutStream

一边写一边读,读连着写

new PipedInputStream().connect(new PipedOutputStream());

 

将流对象写入到实例对象中,然后启动多线程方法

 

三.随即访问文件自身具备读写的方法

RandomAccessFile()

 

四.基本数据对象

DataOutputStream()

文件读写流FileOutputStream()

 

五.操作字节数组

ByteArrayInputStream(),数据源是一个字节数组

因为流对象都是数组对象,无需close()

 

六.常见编码表

ASCII  美国标准信息交换码 一个字节的7位来表示

欧洲码表

GB2312 占两个字节

Unicode 国际标准  两个字节一个字符

GBK 中文升级

UTF-8  三个字节表示一个字符

 

七.字符转换流

InputStreamReader

OutputStreamReader


在整个IO包中,实际上就是分为字节流和字符流,但是除了这两个流之外,还存在了一组字节流-字符流转换类。

OutputStreamWriter:是Writer的子类,将输出的字符变为字节流,即:将一个字符流的输出对象变为字节流的输出对象 ;

InputStreamReader:是Reader的子类,将输入的字节流变为字符流,即:将一个字节流的输入对象变为字符流的输入对象


一般读入InputStreamReader isr=new InputStreamReader(new FileInputStream("gbk.txt"),"GBK");//

一般写入OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("gbk.txt"),"UTF-8");

 

 

八.字符编码

 

字符串变字节数组

String str=”123“;

str.getBytes()

 

字节数组变字符串

new String(str.getBytes(),"ISO8859-1")

 

九练习

import java.io.*;
import java.util.*
class Student implements Comparble<Student>
{
	private String name;
	private int ma,cn,en;
	private int sum;
	Student(String name,int ma,int cn,int en)
	{
		this.name=name;
		this.ma=ma;
		this.cn=cn;
                this.en=en;
		sum=ma+cn+en;
	}
	public int compareTo(Student s)
	{
		int num=new Integer(this.sum).compareTo(new Integer(s.sum));
		if(num==0)
			return this.name.compareTo(s.name);
		return num;
	}

	public String getNmae()
	{
		return name;
	}
	public int getSum()
	{
		return sum;
	}
	public int hashCode()
	{
		return name.hashCode()+sum*78;
	}
	public boolean equlas(Object obj)
	{
		if(!(obj instanceof Student))
			throw new ClassCastException("不匹配");
		Student s=(Student)obj;
		return this.name.equlas(s.name) && this.sum==s.sum;

	}
	public String toString()
	{
		return "student"+name+",";
	}
}
class StudentInfoTool
{
	public static Set<Student> getStudent() throws IOException
	{
		return getStudent(null);
	}
	public static Set<Student> getStudent(Comparator<Student> cmp) throws IOException//指定强行逆转比较器,强行反转的Student的自然排列顺序,其实主要是强行逆转了实现了 Comparable 接口的对象 collection 的自然顺序
	{
		BufferedRead bufr=new BufferedReader(new InputStreamReader(System.in));
		String line=null;
		
		Set<Student> stus=null;
		if(cmp==null)
			stus=new TreeSet<Student>();
		else
			stus=new TreeSet<Student>(cmp);//带比较器的集合。
		while((line=bufr.readLine()!=null)
		{
			if("over".equlas(line))
				break;
			String[] info=line.split(",")
			Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
			stus.add(stu);
			
		}
		
		bufr.close();
		return stus;
	}
	public static void write2File(Set<Student> stus) throws IOException
	{
		BufferedWirte bufw=new BufferedWriter(new FileWriter("stuinfo.txt"))
		for(Student s:stus)
		{
			bufw.write(s.toString());
			bufw.write(s.getSum());
			bufw.newLine();
			bufw.flush();

		}	
		bufw.close();


	}
}
class StudetnDemo
{
	public static void main(String[] args)
	{
		Comparartor<Student> cmp=Collections.reversOrder();
		set<Student> stus=StudentInfoTool.getStudents(cmp);
		StudentInfoToo.write2File(stus);

	}





}


 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值