BloveSaga

在希腊帕尔纳斯山南坡上,有一个驰名世界的戴尔波伊神托所,在它的入口处的巨石上赫然锈刻着这样几个大字: 认识你自己!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  34 随笔 :: 12 文章 :: 122 评论 :: 0 Trackbacks

  编码:将一个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");
 }
}

posted on 2006-06-11 21:27 蓝色Saga 阅读(134) 评论(0)  编辑  收藏 所属分类: Basic Study for JAVA

只有注册用户登录后才能发表评论。


网站导航: