回顾:
五个类:
File类:直接与文件本身有关系;
字节流:OutputStream、InputStream
字符流:Writer、Reader
存放在文件中的都是字节,而读到内存中才可能变为字符;
打印流:PrintStream/PrintWriter:提供了各种打印功能,可以输出任意的内容;
根据实例化对象的不同输出的位置不同;
System.in:键盘输入流、System.out:屏幕输出流;
输入数据的一个基本格式:BufferReader
1、字符集:文件保存时所使用的编码(GBK,GB2312,ISO8859-1)
GBK :包含了繁体和简体的字符集;
GB2312:主要是指简体中文;
ISO8859-1:是国际通用编码;
jvm 默认的文件编码是GBK;
//看看环境的属性
public class Demo{
public static void main(String args[]){
System.getProperties().list(System.out);
}
}
//
import java.io.*;
public class Demo {
public static void main(String args[]) throws Exception{
OutputStream out=null;
out=new FileOutputStream(new File("D:\\java.txt"));
String str="你好";
out.write(str.getBytes());
out.close();
}
}
实际上如果两个的编码不匹配,则在文件包中会出现乱码;
2、SequenceInputStream类:用于合并文件;
将a.txt和b.txt合并成ab.txt;
public SequenceInputStream(InputStream s1,InputStream s2)
接受两个InputStream对象,这就意味着程序需要先使用两个InputStream找到文件;
import java.io.*;
public class Demo {
public static void main(String args[]) throws Exception{
InputStream in1=null;
InputStream in2=null;
//建立一个输出流
OutputStream out=null;
in1=new FileInputStream(new File("D:\\lid1.txt"));
in2=new FileInputStream(new File("D:\\lid2.txt"));
out=new FileOutputStream(new File("D:\\java.txt"));
//此处相当于将两个文件合并
SequenceInputStream seq=null;
seq=new SequenceInputStream(in1,in2);
int c=0;
while((c=seq.read())!=-1){
out.write(c);
}
in1.close();
in2.close();
out.close();
seq.close();
}
}
3、对象序列化:
对象序列化是把对象写到流中,对象反序列话是从流中恢复对象;
对象序列化的优点在于即使程序结束,对象仍然可以被保存下来(保存在了文件中);
对象所在的类必须实现java.io.Serializable接口,才能被序列化;
如果需要将对象变为byte流,则需要将对象进行转化:ObjectOutputString实现
此类的构造方法类似于PrintWrite/PrintStream,谁为ObjectOutputStream实例化,就向
那里输出。
反序列化用到ObjectInputStream,必须明确告诉ObjectInputStream 去那里读;
//创建一个Person类,把Person类的对象序列化
import java.io.*;
class Person implements Serializable{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age;
}
}
public class Demo{
public static void main(String agrs[]) throws Exception{
//Person per=new Person("lid",20);
//ser(per);
System.out.println(dser());
}
//创建一个方法,完成对象的序列化
public static void ser(Person per) throws Exception{
ObjectOutputStream oos=null;
oos=new ObjectOutputStream(new FileOutputStream(new File("D:\\java.txt")));
oos.writeObject(per);
oos.close();
}
//反序列化
public static Person dser() throws Exception{
ObjectInputStream ois=null;
ois=new ObjectInputStream(new FileInputStream(new File("D:\\java.txt")));
Object obj=ois.readObject();
ois.close();
return (Person)obj;
}
}
一个序列化的核心部分是:Serializable接口
问题:在Person中我们没有覆写任何一个方法;
次接口中没有任何方法,此接口只是一个声明性的接口,表示实现此接口的类可以被序列化;
问题:现在不希望年龄不想被序列化:
当不希望类中的某个字段被序列化的时候,用 transient;
//创建一个Person类,把Person类的对象序列化
import java.io.*;
class Person implements Serializable{
private String name;
private transient int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age;
}
}
public class Demo{
public static void main(String agrs[]) throws Exception{
//Person per=new Person("lid",20);
//ser(per);
System.out.println(dser());
}
//创建一个方法,完成对象的序列化
public static void ser(Person per) throws Exception{
ObjectOutputStream oos=null;
oos=new ObjectOutputStream(new FileOutputStream(new File("D:\\java.txt")));
oos.writeObject(per);
oos.close();
}
//反序列化
public static Person dser() throws Exception{
ObjectInputStream ois=null;
ois=new ObjectInputStream(new FileInputStream(new File("D:\\java.txt")));
Object obj=ois.readObject();
ois.close();
return (Person)obj;
}
}
变量声明的完整定义:
[public|protect|private][final][static][transient] 数据类型 变量名称 [=初始化内容;]
完整的方法定义
[public|protect|private][final][static][synchronized] 返回类型|void 方法名[参数]