? 編碼:將一個Unicode碼轉換為本地字符表示的過程為編碼。
? 解碼:將一個字節轉換為一個字符(用Unicode表示),這個過程叫解碼。
??????? [簡單的說去獲取一個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類同時實現了DataInput和DataOutput接口,提供了對文件隨機存取的功能,
? 利用這個類可以在文件的任何位置讀取或寫入數據。
? RandomAccessFile類提供了一個文件指針,用來標志要進行讀寫操作的下一位數據的位置。
?
?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();??
?}
}
?????????? 對象序列化
?.將對象轉換為字節流保存起來,并在日后還原這個對象,這種機制叫做對象序列化。
?.將一個對象保存到永久存儲設備上稱為持續性。
?.一個對象要想能夠實現序列化,必須實現Serializable接口或Externalizable接口。
?.當一個對象被序列化時,只保存對象的非靜態成員變量,不能保存任何的成員變量和靜態的
? 成員變量。
?.如果一個對象的成員變量是一個對象,那么這個對象的數據成員也會被保存。
?.如果一個可序列化的對象包含對某個不可序列化的對象的引用,那么整個序列化操作將會失敗,
? 并且會拋出一個NotSerializableException。我們可以將這個引用標記為transient,那么對象
? 仍然可以序列化。
?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;
?}
?//可以寫private void readObject()方法來控制我們自己想要實現的
?private void writeObject(java.io.ObjectOutputStream oos)throws Exception
?{
??//例如我們自己寫想要顯示的順序和那些需要顯示
??oos.writeInt(age);
??oos.writeUTF(name);
??System.out.println("Write Object");
?}
?private void readObject(java.io.ObjectInputStream ois)throws Exception
?{
??//按照寫入的順序來讀取
??age=ois.readInt();
??name=ois.readUTF();
??System.out.println("Read Object");
?}
}