BloveSaga

          在希臘帕爾納斯山南坡上,有一個馳名世界的戴爾波伊神托所,在它的入口處的巨石上赫然銹刻著這樣幾個大字: 認(rèn)識你自己!

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            34 隨筆 :: 12 文章 :: 122 評論 :: 0 Trackbacks

          ? 編碼:將一個Unicode碼轉(zhuǎn)換為本地字符表示的過程為編碼。
          ? 解碼:將一個字節(jié)轉(zhuǎn)換為一個字符(用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類同時實現(xiàn)了DataInput和DataOutput接口,提供了對文件隨機(jī)存取的功能,
          ? 利用這個類可以在文件的任何位置讀取或?qū)懭霐?shù)據(jù)。
          ? RandomAccessFile類提供了一個文件指針,用來標(biāo)志要進(jìn)行讀寫操作的下一位數(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();??
          ?}
          }


          ?????????? 對象序列化

          ?.將對象轉(zhuǎn)換為字節(jié)流保存起來,并在日后還原這個對象,這種機(jī)制叫做對象序列化。
          ?.將一個對象保存到永久存儲設(shè)備上稱為持續(xù)性。
          ?.一個對象要想能夠?qū)崿F(xiàn)序列化,必須實現(xiàn)Serializable接口或Externalizable接口。
          ?.當(dāng)一個對象被序列化時,只保存對象的非靜態(tài)成員變量,不能保存任何的成員變量和靜態(tài)的
          ? 成員變量。
          ?.如果一個對象的成員變量是一個對象,那么這個對象的數(shù)據(jù)成員也會被保存。
          ?.如果一個可序列化的對象包含對某個不可序列化的對象的引用,那么整個序列化操作將會失敗,
          ? 并且會拋出一個NotSerializableException。我們可以將這個引用標(biāo)記為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()方法來控制我們自己想要實現(xiàn)的
          ?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");
          ?}
          }

          posted on 2006-06-11 21:27 藍(lán)色Saga 閱讀(153) 評論(0)  編輯  收藏 所屬分類: Basic Study for JAVA
          主站蜘蛛池模板: 福鼎市| 南漳县| 鄂温| 东台市| 高雄县| 尼玛县| 松原市| 电白县| 平武县| 哈密市| 武邑县| 哈尔滨市| 大竹县| 台东市| 渭南市| 措美县| 鹿泉市| 满城县| 曲沃县| 玛纳斯县| 正定县| 临高县| 新安县| 长岭县| 股票| 和硕县| 蓬安县| 苍南县| 丰宁| 嘉鱼县| 登封市| 浙江省| 巴中市| 繁峙县| 柘荣县| 赫章县| 紫云| 怀来县| 南汇区| 垫江县| 德江县|