编码:
(1)utf-8编码中文占用3个字节,英文占用一个字节。
(2)gbk编码中文占用2个字节,英文占用1个字节。
(3)java是双字节编码utf-16be,中文占用2个字节,英文占用2个字节。
序列化:
序列化就是将Object转换成byte序列,反之叫对象的反序列化。序列化流(ObjectOutputStream)--writeObject,反序列化流(ObjectInputStream)--readObject
对象必须实现序列化接口(Serializable),才能进行序列化,否则将出现异常。这个接口没有任何方法,只是一个标准。
序列化对象的代码模板:
//序列化对象
OutputStream os = new FileOutputStream("C:\\Users\\User\\Desktop\\论文\\wzf.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
Student stu = new Student("wzf",26);
oos.writeObject(stu);
oos.flush();
oos.close();
os.close();
//反序列化对象
InputStream is = new FileInputStream("C:\\Users\\User\\Desktop\\论文\\wzf.txt");
ObjectInputStream ois = new ObjectInputStream(is);
Student stu2 = (Student)ois.readObject();
System.out.println(stu2);
ois.close();
is.close();
读写文件的代码模板:
InputStream is = new FileInputStream("C:\\Users\\User\\Desktop\\论文\\坐标文件\\Car19_utf.txt");
OutputStream os = new FileOutputStream("C:\\Users\\User\\Desktop\\论文\\坐标文件\\wzf.txt");
InputStreamReader isr = new InputStreamReader(is,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(os,"gbk");
BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(osw);
String str;
while((str = br.readLine()) != null){
//osw.write(str+"\r\n");
bw.write(str);
bw.newLine();
osw.flush();
}
bw.close();
br.close();
osw.close();
isr.close();
os.close();
is.close();