您的位置:首页 > 编程语言 > Java开发

java之路,IO操作(字符集,序列化)

2013-11-22 12:27 537 查看
字符集的编码

n ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符、数字符号被计算机处理时都是以二进制码的形式出现的。这种二进制码的集合就是所谓的ASCII码。每一个ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。

n GB2312:GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字符集-基本集》。主要用于给每一个中文字符指定相应的数字,也就是进行编码。一个中文字符用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1来表示。

n GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是“扩展”的汉语拼音第一个字母)。在新的编码系统里,除了完全兼容GB2312 外,还对繁体中文、一些不常用的汉字和许多符号进行了编码。

n ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集 ,而英文实际上只用了其中数字小于128的部分。

n Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。

n UTF-8:Eight-bit
UCS Transformation Format,(UCS,Universal
Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节。

import java.util.*;

import java.nio.charset.*;

class CodeTest

{

public static void main(String[] args) throws Exception

{/*

Map m = Charset.availableCharsets(); //静态方法,返回当前可用的字符集

Set names = m.keySet(); //返回键值对

Iterator it = names.iterator(); //没有get方法,用迭代器来实现

while(it.hasNext())

{

System.out.println(it.next());

}



Properties pps = System.getProperties(); //获取当前默认字符集

pps.list(System.out); //list方法将其打印到指定的输出设备,System.out是标准输出

*/



Properties pps = System.getProperties();

pps.put("file.encoding", "ISO-8859-1"); //修改java虚拟机默认的编码方式

int data, i = 0;

byte[] buf = new byte[100];

while((data = System.in.read()) != 'q') //获取输入

{

buf[i] = (byte)data;

i++;

}

String str = new String(buf, 0, i); //构造字符串

System.out.println(str);

String strGBK = new String(str.getBytes("GBK"), "GBK");//按照指定的编码方式构造字符串

System.out.println(strGBK);

}

}

RandomAccessFile

n RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。

n RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。

import java.io.*;

class RandomFileTest

{

public static void main(String[] args)

{

Student s1 = new Student(1, "zhangsan", 88);

Student s1 = new Student(2, "lisi", 89);

Student s1 = new Student(3, "wangwu", 80);



RandomAccessFile raf = RandomAccessFile("student.txt", "rw");

s1.writeStudent(raf);

s2.writeStudent(raf);

s3.writeStudent(raf);

raf.close();

}

}

class Student

{

int num;

String name;

double score;



public Student(int num, String name, double score)

{

this.num = num;

this.name = name;

this.score = score;

}



public void writeStudent(RandomAccessFile raf) throws IOException

{

raf.writeInt(num);

raf.writeUTF(name);

raf.writeDouble(score);

}



public void readStudent(RandomAccessFile raf) throws IOException

{

raf.readInt(num);

raf.readUTF(name);

raf.readDouble(score);

}

}

import java.io.*;

class RandomTest

{

public static void main(String[] args) throws IOException

{

Student s1 = new Student(1, "zhangsan", 88);

Student s2 = new Student(2, "lisi", 89);

Student s3 = new Student(3, "wangwu", 80);



RandomAccessFile raf = new RandomAccessFile("student.txt", "rw");

s1.writeStudent(raf);

s2.writeStudent(raf);

s3.writeStudent(raf);



Student s = new Student();

//getFilePointer获取文件指针

//seek设置文件指针

raf.seek(0);

for(long i=0; i<raf.length(); i=raf.getFilePointer())

{

s.readStudent(raf);

System.out.println(s.num + ":" + s.name + ":"

+ s.score);

}

raf.close();

}

}

class Student

{

int num;

String name;

double score;



public Student()

{



}

public Student(int num, String name, double score)

{

this.num = num;

this.name = name;

this.score = score;

}



public void writeStudent(RandomAccessFile raf) throws IOException

{

raf.writeInt(num);

raf.writeUTF(name);

raf.writeDouble(score);

}



public void readStudent(RandomAccessFile raf) throws IOException

{

raf.readInt();

raf.readUTF();

raf.readDouble();

}

}

对象序列化

n 将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化。

n 将一个对象保存到永久存储设备上称为持续性。

n 一个对象要想能够实现序列化,必须实现Serializable接口或Externalizable接口。

n 当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。

n 如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。

n 如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化。

import java.io.*;

class ObjectSerialTest

{

public static void main(String[] args) throws Exception

{

Employee e1 = new Employee("zhangsan", 20, 2000);

Employee e2 = new Employee("lisi", 21, 2100);

Employee e3 = new Employee("wangwu", 22, 2200);



FileOutputStream fos = new FileOutputStream("employee.txt");

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(e1); //序列化是调用writeObject

oos.writeObject(e2);

oos.writeObject(e3);

oos.close();



FileInputStream fis = new FileInputStream("employee.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

Employee e;

for(int i=0; i<3; i++)

{

e = (Employee)ois.readObject();//反序列化

System.out.println(e.name+":"+e.age+":"+e.salary);

}

}

}

class Employee implements Serializable

{

String name;

int age;

double salary;



public Employee(String name, int age, double salary)

{

this.name = name;

this.age = age;

this.salary = salary;

}

//重写writeObject方法,按照自己的方式来序列化

private void writeObject(java.io.ObjectOutputStream oos) throws IOException

{

oos.writeInt(age);

oos.writeUTF(name);

System.out.println("write object");

}

//重写readObject方法,虽然是私有的方法,但是可以在外部调用,这是两个特例

private void readObject(java.io.ObjectOutputStream ois) throws IOException

{

age = ois.readInt();

name = ois.readUTF();

System.out.println("read object");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: