-
package com.imooc.io; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSeriaDemo2 { public static void main(String[] args) throws Exception { // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( // "demo/obj1.dat")); // Foo2 foo2 = new Foo2(); // oos.writeObject(foo2); // oos.flush(); // oos.close(); // 反序列化是否递归调用到父类的函数 // ObjectInputStream ois = new ObjectInputStream(new FileInputStream( // "demo/obj1.dat")); // Foo2 foo2 = (Foo2)ois.readObject(); // System.out.println(foo2); // ois.close();查看全部
-
// ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( // "demo/obj1.dat")); // Bar2 bar2 = new Bar2(); // oos.writeObject(bar2); // oos.flush(); // oos.close(); // 反序列化是否递归调用到父类的函数 ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "demo/obj1.dat")); Bar2 bar2 = (Bar2) ois.readObject(); System.out.println(bar2); ois.close();查看全部
-
/* * 对子类对象进行反序列化操作时, * 如果其父类没有实现序列化接口, * 那么其父类的构造函数会被调用 */ } } /* * 一个类实现了序列化接口,那么其子类都可以进行序列化 */ class Foo implements Serializable { public Foo() { System.out.println("foo……"); } } class Foo1 extends Foo { public Foo1() { System.out.println("foo1……"); } } class Foo2 extends Foo1 { public Foo2() { System.out.println("foo2……"); } } class Bar { public Bar() { System.out.println("bar"); } } class Bar1 extends Bar{ public Bar1() { System.out.println("bar1……"); } } class Bar2 extends Bar1 implements Serializable { public Bar2() { System.out.println("bar2……"); } }查看全部
-
5)序列化中 子类和父类构造函数的调用问题查看全部
-
3、对象的序列化、反序列化 1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化。 2)序列化流(ObjectOutputStream)是过滤流 --> writeObject 反序列化流(ObjectInputStream) -->readObject 3) 序列化接口(Serializable) 对象必须实现序列化接口,才能进行序列化,否则将出现异常 这个接口,没有任何方法,只是一个标准 4) transient 关键字 private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException private void readObject(java.io.ObjectInputStream s) throws java.io.IOException,ClassNotFoundException查看全部
-
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject();//把jvm能默认序列化的元素进行序列化操作 s.writeInt(stuage);//自己完成stuage的序列化 } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException,ClassNotFoundException{ s.defaultReadObject();//把jvm能默认反序列化的元素进行序列化操作 this.stuage = s.readInt();//自己完成stuage的反序列化操作 }查看全部
-
package com.imooc.io; // 文件传输基础——Java IO流...6-1 序列化基本操作 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectSeriaDemo1 { public static void main(String[] args)throws Exception { String file = "demo/obj.dat"; //1.对象的序列化 // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); // Student stu = new Student("10001","张三",20); // oos.writeObject(stu); // oos.flush(); // oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Student stu =(Student)ois.readObject(); System.out.println(stu); ois.close(); } }查看全部
-
为什么我的运行结果是错的? package com.imooc.io; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectSeriaDemo1 { public static void main(String[] args)throws Exception { String file = "demo/obj.dat"; //1.对象的序列化 // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); // Student stu = new Student("10001","张三",20); // oos.writeObject(stu); // oos.flush(); // oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Student stu =(Student)ois.readObject(); System.out.println(stu); ois.close(); } }查看全部
-
java 是 双字节编码 是 utf-16be查看全部
-
在gbk编码中中文占两个字节,英文占一个字节,在utf-8中 中文占三个字节 英文还是占一个字节 utf-16be 中文占两个字节 英文还是占两个字节查看全部
-
package com.imooc.io; public class Student { private String stunum; private String stuname; private int stuage; public Student(){ } public Student(String stunum, String stuname, int stuage) { super(); this.stunum = stunum; this.stuname = stuname; this.stuage = stuage; } public String getStunum() { return stunum; } @Override public String toString() { return "Student [stunum=" + stunum + ", stuname=" + stuname + ", stuage=" + stuage + "]"; } public void setStunum(String stunum) { this.stunum = stunum; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public int getStuage() { return stuage; } public void setStuage(int stuage) { this.stuage = stuage; } }查看全部
-
// PrintWriter pw1 = new PrintWriter(outputStream, blooean autoFlush);查看全部
-
package com.imooc.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class BrAndBwOrPwDemo { public static void main(String[] args) throws IOException { // 对文件进行读写操作 BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream("e:\\javaio\\file.txt"))); // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( // new FileOutputStream("e:\\javaio\\file3.txt"))); PrintWriter pw = new PrintWriter("e:\\javaio\\file4.txt"); String line; while ((line = br.readLine()) != null) { System.out.println(line);// 一次读一行,并不能识别换行 // bw.write(line); // //单独写出换行操作 // bw.newLine();//换行操作 // bw.flush(); pw.println(line); pw.flush(); } br.close(); // bw.close(); pw.close(); } }查看全部
-
package com.imooc.io; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FrAndFwDemo { public static void main(String[] args)throws IOException { FileReader fr = new FileReader("e:\\javaio\\imooc1.txt"); FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt"); // FileWriter fw = new FileWriter("e:\\javaio\\imooc2.txt",true);//往里追加内容 char[] buffer = new char[2056]; int c; while((c = fr.read(buffer,0,buffer.length))!=-1){ fw.write(buffer,0,c); fw.flush(); } fr.close(); fw.close(); } }查看全部
-
package com.imooc.io; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class IsrAndOsrDemo { public static void main(String[] args)throws IOException { FileInputStream in = new FileInputStream("e:\\javaio\\imooc.txt"); InputStreamReader isr = new InputStreamReader(in); // 默认项目的编码 FileOutputStream out = new FileOutputStream("e:\\javaio\\FileUtilsutf-8.txt"); OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8"); /*int c; while((c = isr.read())!=-1){ System.out.print((char)c); }*/ char[] buffer = new char[8*1024]; int c ; /* * 批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个 * 返回的是读到的字符的个数 */ while((c = isr.read(buffer,0,buffer.length))!=-1){ String s = new String(buffer,0,c); System.out.print(s); osw.write(buffer,0,c); osw.flush(); } isr.close(); osw.close(); } }查看全部
举报
0/150
提交
取消