ObjectOutputStream。java
ObjectOutputStream fw= new数组 ObjectOutputStream( new FileOutputStream("F:\\aaa\\a.txt"));ide |
接口的类的对象均可以被序列化。若是没有实现此接口,那么这个类的对象就不能学习
被序列化或者反序列化,不然会抛出 NotSerializableException。测试
注明是瞬态的,使用 transient 关键字修饰,则这个属性就不能被序列化。this
public class Student implements Serializable {编码 private static final long serialVersionUID = 727935469565319794L;idea private String name;spa private String address;.net private Integer age; private Integer score;
public Student(){
}
public Student(String name, String address, Integer age, Integer score) { this.name = name; this.address = address; this.age = age; this.score = score; }
public static long getSerialVersionUID() { return serialVersionUID; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public Integer getScore() { return score; }
public void setScore(Integer score) { this.score = score; }
public String toString() { return "Student{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", age=" + age + ", score=" + score + '}'; } }
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;
//序列化父类java.io.OutputStream 子类:FileOutputStream // 序列化:java.io.ObjectOutputStream继承了OutputStream类 public class Meth04 { public static void main(String[] args) {
//构造方法 ObjectOutputStream(OutputStream out) //建立一个写入指定的OutputStream的ObjectOutputStream。
//建立序列化对象 //ObjectOutputStream fw=new ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));
//建立序化的对象 Student student=new Student("王智雅","山西市",20,99); Student student1=new Student("李文杰","天水市",23,98); Student student2=new Student("桑凤娇","泰安市",24,99); Student student3=new Student("郭朝旭","聊城市",25,96); Student student4=new Student("陈天意","承德市",27,100);
try(ObjectOutputStream fw=new ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));){ fw.writeObject(student); fw.writeObject(student1); fw.writeObject(student2); fw.writeObject(student3); fw.writeObject(student4);
}catch (FileNotFoundException ce ){ ce.printStackTrace(); }catch (IOException ce){ ce.printStackTrace(); } } |
ObjectInputStream (InputStream in):建立从指的InputStream 读取的 ObjectInputStream。
//反序列化父类 java.io.InputStream 子类时FileinputStream //java.io.ObjectInputStream 继承了InputStream
//反序列化示例: public class Meth05 { public static void main(String[] args) { //建立反序列化的对象 // ObjectInputStream object=new //ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));
//建立Student 对象 Student student=new Student(); //读文件 try(ObjectInputStream object=new ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));){ Student ss=(Student)object.readObject(); while(ss!=null){ System.out.println(ss); ss=(Student)object.readObject(); } }catch(ClassNotFoundException ce){ ce.printStackTrace(); }catch (IOException ce){
} } } /* 运行的结果: Student{name='王智雅', address='山西市', age=20, score=99} Student{name='李文杰', address='天水市', age=23, score=98} Student{name='桑凤娇', address='泰安市', age=24, score=99} Student{name='郭朝旭', address='聊城市', age=25, score=96} Student{name='陈天意', address='承德市', age=27, score=100}*/ |
当 JVM 反序列化对象时,抛出了一个 InvalidClassException 异常,发生这个异常的缘由以下:
一、 该类的序列化版本号与从流中读取的类描述的版本不匹配
二、 该类包含未知数据类型Serializable 接口给须要序列化的类,提供了一个序列化版本号,serialVersionUID 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。
上一章咱们学习了字节流和字符流,接下来咱们学习一些更强大的流,好比:可以高效读写
的缓冲流,可以转换编码的转换流,可以持久化存储对象的序列化流等待。这些功能更为强
大的流,都是在基本流对象基础之上建立而来的,这些功能更强大的流,至关因而对基本流
对象的一种加强。
缓冲流的基本原理: 是在建立流对象时,会建立一个内置的默认大小的缓冲区数组,经过缓冲区读写,(往内存中读写数据是最块的),减小系统IO的次数,从而提升读写的效率。
//普通的流实现数据的传送:消耗的时间:16193毫秒 public class Meth06 { public static void main(String[] args) { //开始时间 long start=System.currentTimeMillis(); try(FileInputStream input=new FileInputStream("E:\\idea64.exe"); FileOutputStream output=new FileOutputStream("F:\\copy.exe");){ int i; while ((i=input.read())!=-1){ output.write(i); }
}catch (IOException ce){ ce.printStackTrace(); } long end=System.currentTimeMillis();
System.out.println("复制普通文件的消耗的时间:"+(end-start)+"毫秒"); } } |
// BufferedStream字节缓冲流实现传送:消耗的时间:94毫秒 public class Meth07 { public static void main(String[] args) { //开始时间 long start=System.currentTimeMillis(); try(BufferedInputStream fr=new BufferedInputStream(new FileInputStream("E:\\idea64.exe")); BufferedOutputStream fw=new BufferedOutputStream(new FileOutputStream("F:\\copy1.exe"))){ int i; while ((i=fr.read())!=-1){ fw.write(i); }
}catch (IOException ce){ ce.printStackTrace(); } //结时间 long end=System.currentTimeMillis(); System.out.println("复制普通文件的消耗的时间:"+(end-start)+"毫秒"); } } |
// BufferedStream字节缓冲流用数组实现实现复制,更快 复制普通文件的消耗的时间:16毫秒 public class Meth08 { public static void main(String[] args) { //开始时间 long start=System.currentTimeMillis(); try(BufferedInputStream fr=new BufferedInputStream(new FileInputStream("E:\\idea64.exe")); BufferedOutputStream fw=new BufferedOutputStream(new FileOutputStream("F:\\copy2.exe"))){ int i; //使用数组 byte[]bytes=new byte[1024*8]; while ((i=fr.read(bytes))!=-1){ fw.write(bytes,0,i); }
}catch (IOException ce){ ce.printStackTrace(); } long end=System.currentTimeMillis(); System.out.println("复制普通文件的消耗的时间:"+(end-start)+"毫秒"); } } |
1 字符缓冲流:BufferedReader 读
//BufferedReader 字符缓冲流读操做 public class Meth09 { public static void main(String[] args)throws IOException{
BufferedReader fr=new BufferedReader(new FileReader("F:\\aaa\\a.txt")); String s=null; //每次读取一行的内容 while ((s=fr.readLine())!=null){ System.out.println(s); } fr.close(); } } |
2 字符缓冲流: BufferedWriter 写
//BufferedWriter 字符缓冲流 写 public class Meth10 { public static void main(String[] args)throws IOException{ BufferedWriter fw=new BufferedWriter(new FileWriter("F:\\aaa\\a.txt")); fw.write(65); //实现换行 fw.newLine(); fw.write("李文杰我爱个人祖国"); fw.newLine(); fw.write("hgsahgd"); fw.newLine(); fw.close(); } } |
1计算机中储存的信息都是用二进制表示的,而咱们在屏幕上看到的数字,英文,标点符号、
汉字等字符是二进制数转换以后的结果。将字符存储到计算机中,称为编码。反之,将存储在计算机中的二进制数按照某种规则解析出来,称为解码。好比说,按照 A 规 则存储,一样按照 A 规则解析,那么就能显示正确的文本符号。反之,按照 A 规则存储,再
按照 B 规则解析,就会致使乱码现象。
2 字符编码(Character Encoding)就是一套天然语言的字符与二进制数之间的对应规则。
127 的字符连在一块儿就表示一个汉字,这样大约能够组合了包含 7000 多个简体汉字。
共收录 20000 多个汉字,兼容 GB2312。
//转换流 InputStreamReader,将字节转换为字符,实现读取的操做 public class Meth11 { public static void main(String[] args)throws IOException{ //指定字符的编UF码 InputStreamReader input=new InputStreamReader(new FileInputStream("F:\\aaa\\a.txt"),"UTF-8"); int i; while ((i=input.read())!=-1){ System.out.print((char)i); } input.close(); } } |
//转换流 OutputStreamWriter 将字符转换为字节,实现存储的操做(写) public class Meth12 { public static void main(String[] args)throws IOException{ //指定字符的编码 OutputStreamWriter fr=new OutputStreamWriter(new FileOutputStream("F:\\aaa\\a.txt"),"UTF-8"); fr.write("李文杰@1314520"); fr.write("sangfenjirjie"); fr.write("liwnejie"); fr.close(); } } |
//打印流,只有输出流,没有输入流 public class Meth13 { public static void main(String[] args)throws IOException{ //static void setOut(PrintStream out);打印流 PrintStream print=new PrintStream("F:\\aaa\\a.txt"); System.setOut(print); System.out.println("liwnejie"); System.out.println("sangfengjiao"); System.out.println("wangzhiys"); } } |