? 編碼:將一個(gè)Unicode碼轉(zhuǎn)換為本地字符表示的過(guò)程為編碼。
? 解碼:將一個(gè)字節(jié)轉(zhuǎn)換為一個(gè)字符(用Unicode表示),這個(gè)過(guò)程叫解碼。
??????? [簡(jiǎn)單的說(shuō)去獲取一個(gè)Unicode碼就是解碼]
?code:
import java.util.*;
import java.nio.charset.*;
class CharsetTest
{
?public static void main(String[] args)throws Exception
?{
??/*
??Map m=Charset.availableCharsets();
??Set names=m.keySet();
??Iterator it =names.iterator();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
??*/
??Properties pps=System.getProperties();
??//pps.list(System.out);
??pps.put("file.encoding","ISO-8859-1");
??int data;
??byte[] buf=new byte[100];
??int i=0;
??while((data=System.in.read())!='q')
??{
???buf[i]=(byte)data;
???i++;
??}
??String str=new String(buf,0,i);
??//String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
??//System.out.println(strGBK);
??System.out.println(str);
?}
}
?
???? RandomAccessFile
? RandomAccessFile類同時(shí)實(shí)現(xiàn)了DataInput和DataOutput接口,提供了對(duì)文件隨機(jī)存取的功能,
? 利用這個(gè)類可以在文件的任何位置讀取或?qū)懭霐?shù)據(jù)。
? RandomAccessFile類提供了一個(gè)文件指針,用來(lái)標(biāo)志要進(jìn)行讀寫(xiě)操作的下一位數(shù)據(jù)的位置。
?
?code:
import java.io.*;
class RandomFileTest
{
?public static void main(String[] args)throws Exception
?{
??Student s1 = new Student(1,"zhangsan",98.5);
??Student s2 = new Student(2,"lisi",90.5);
??Student s3 = new Student(3,"wangwu",78.5);
??
??RandomAccessFile rsf=new RandomAccessFile("student.txt","rw");? //存取模式rw
??s1.WriteStudent(rsf);
??s2.WriteStudent(rsf);
??s3.WriteStudent(rsf);
??
??Student s =new Student();
??rsf.seek(0); //把文件指針移到文件首
??for(long i=0;i<rsf.length();i=rsf.getFilePointer())
??{
???s.ReadStudent(rsf);
???System.out.println(s.num+":"+s.name+":"+s.score);
??}
??rsf.close();
?}
}
class Student
{
?int num;
?String name;
?double score;
?Student()
?{
??
?}
?Student(int num,String name,double score)
?{
??this.num=num;
??this.name=name;
??this.score=score;
?}
?public void WriteStudent(RandomAccessFile raf)throws Exception
?{
??raf.writeInt(num);
??raf.writeUTF(name);
??raf.writeDouble(score);
?}
?public void ReadStudent(RandomAccessFile raf)throws Exception
?{
??raf.readInt();
??raf.readUTF();
??raf.readDouble();??
?}
}
?????????? 對(duì)象序列化
?.將對(duì)象轉(zhuǎn)換為字節(jié)流保存起來(lái),并在日后還原這個(gè)對(duì)象,這種機(jī)制叫做對(duì)象序列化。
?.將一個(gè)對(duì)象保存到永久存儲(chǔ)設(shè)備上稱為持續(xù)性。
?.一個(gè)對(duì)象要想能夠?qū)崿F(xiàn)序列化,必須實(shí)現(xiàn)Serializable接口或Externalizable接口。
?.當(dāng)一個(gè)對(duì)象被序列化時(shí),只保存對(duì)象的非靜態(tài)成員變量,不能保存任何的成員變量和靜態(tài)的
? 成員變量。
?.如果一個(gè)對(duì)象的成員變量是一個(gè)對(duì)象,那么這個(gè)對(duì)象的數(shù)據(jù)成員也會(huì)被保存。
?.如果一個(gè)可序列化的對(duì)象包含對(duì)某個(gè)不可序列化的對(duì)象的引用,那么整個(gè)序列化操作將會(huì)失敗,
? 并且會(huì)拋出一個(gè)NotSerializableException。我們可以將這個(gè)引用標(biāo)記為transient,那么對(duì)象
? 仍然可以序列化。
?code:
import java.io.*;
class ObjectSerialTest
{
?public static void main(String[] args)throws Exception
?{
??Employee e1 = new Employee("zhangsan",20,2800.50);
??Employee e2 = new Employee("lisi",22,25000.50);
??Employee e3 = new Employee("wangwu",23,12800.50);
??Employee e4 = new Employee("blovesaga",22,3800.50);
??
??FileOutputStream fos=new FileOutputStream("employee.txt");
??ObjectOutputStream oos=new ObjectOutputStream(fos);
??oos.writeObject(e1);
??oos.writeObject(e2);
??oos.writeObject(e3);
??oos.writeObject(e4);
??oos.close();
??
??FileInputStream fis = new FileInputStream("employee.txt");
??ObjectInputStream ois =new ObjectInputStream(fis);
??Employee e;
??for(int i=0;i<4;i++)
??{
???e=(Employee)ois.readObject();
???System.out.println(e.name+":"+e.age+":"+e.salary);
??}
??ois.close();
?}
}
class Employee implements Serializable
{
?String name;
?int age;
?double salary;
?transient Thread t1 =new Thread();
?Employee(String name,int age,double salary)
?{
??this.name=name;
??this.age=age;
??this.salary=salary;
?}
?//可以寫(xiě)private void readObject()方法來(lái)控制我們自己想要實(shí)現(xiàn)的
?private void writeObject(java.io.ObjectOutputStream oos)throws Exception
?{
??//例如我們自己寫(xiě)想要顯示的順序和那些需要顯示
??oos.writeInt(age);
??oos.writeUTF(name);
??System.out.println("Write Object");
?}
?private void readObject(java.io.ObjectInputStream ois)throws Exception
?{
??//按照寫(xiě)入的順序來(lái)讀取
??age=ois.readInt();
??name=ois.readUTF();
??System.out.println("Read Object");
?}
}