第十天:序列化 乱码原理 反射
序列化:通俗来讲就是把数据写进文件中去
反序列化:通俗来讲就是把文件中的数据读出来
1..1 使用序列化流把数据写进文件中去
ObjectOutPutStearm
例子:
public static void main(String[] args) throws Exception{ /*使用序列化流把对象写入文件*/ //创建输出流对象 OutputStream os=new FileOutputStream("a.txt"); ObjectOutputStream oos=new ObjectOutputStream(os); //创建学生对象 Student s1 = new Student(1, "张三丰", 20, "106"); Student s2 = new Student(2, "小李", 28, "106"); Student s3= new Student(3, "小张", 38, "106"); Student s4 = new Student(4, "左禅", 40, "106"); //把对象直接写入文件 /* void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。 */ oos.writeObject(s1);//NotSerializableException---没有序列化异常 oos.writeObject(s2); oos.writeObject(s3); oos.writeObject(s4); /*如果象使用对象流直接操作对象,此对象的类必须序列化 * 要实现Serializable接口*/ oos.close(); }
1.2 使用反序列化流把数据读出来
ObjectInPutStearm
例子:
public static void main(String[] args) throws Exception{ /*使用反序列化流读对象*/ ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt")); /* Object readObject() 从 ObjectInputStream 读取对象。 */ /*Student s = (Student) ois.readObject(); System.out.println(s); Student s1 = (Student) ois.readObject(); System.out.println(s1);*/ //EOFException 当使用反序列流读对象时,当没有更多的对象可读时,不是返回-1,也不是返回null, //而是直接报错。 //以什么条件做为临界点 while(true){ try { Student s2 = (Student) ois.readObject(); System.out.println(s2); } catch (EOFException e) { //当出现异常时,不需要再继续往下读了,退出循环 break; } } }
2.出现序列码异常(不匹配)
解决方法:需要我们在类中手动设置序列码
private static final long serialVersionUID = 1L;
3.乱码
产生原因:编码与解码用到的编码表不一样 具体如图:
怎么去解决?
1.改变idea的编码格式(不建议)
改变idea的编码格式,之前的代码如果是跟过来的编码格式不一致,其他模块下的代码就会产生乱码
2.改变文本的编码格式(不建议)
3.通过代码的设置解决乱码问题(使用转换流)
乱码问题原理:
编码与解码用到的编码表不一样
2.反射:java提供的一种技术,可以获取类中非公共成员的技术(包含私有的成员)
2.1反射技术的实现依赖于类的字节码对象
生成字节码对象的三种方式:
1.类名.class
2.对象名.getClass()
3.Class.forName("全限制类名")
三种方式代码示例:
public static void main(String[] args) throws ClassNotFoundException { //第一种方式 Class<Student> class1= Student.class; //第二种---依赖于对象生成 // Student student = new Student(); // Class<? extends Student> aClass = student.getClass() //第三种----Class.forName("com.swlz.fanse.Student")里面是类的全限制类名 建议使用第三种 Class.forName("com.swlz.fanse.Student"); }
2.2什么是字节码文件?
.java文件通过javac编译成.class文件,这就是字节码文件
2.3什么是字节码对象?
我们所有的字节码文件,被封装成一个字节码类,那么单个的字节码类就可以看成是一个字节码对象
2.4通过反射获取类中的构造方法
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { //通过反射获取类的字节码对象 Class<?> aClass = Class.forName("com.swlz.fanse.Student"); //通过类的字节码对象获取类的无参构造函数 Constructor<?> classConstructor = aClass.getConstructor(); System.out.println(classConstructor); 通过类的字节码对象获取类的有参构造函数 Constructor<?> classConstructor1 = aClass.getConstructor(String.class); System.out.println(classConstructor1); //用来获取所有的公共构造方法aClass.getConstructors() Constructor<?>[] 数组 Constructor<?>[] constructors = aClass.getConstructors(); for (Constructor<?> constructor : constructors) { System.out.println(constructor); } }
2.5通过反射获取类的字段:
例子:
public static void main(String[] args) throws Exception{ /*通过反射技术获取字段(属性) * */ // method1(clazz); //获取字节码对象 Class clazz = Class.forName("com.bianyiit.fanshe.Student"); //获取非公共的字段对象 /*Field getDeclaredField(String name) 返回一个 Field 对象,获取公共的与非公共的字段对象。 */ Field age = clazz.getDeclaredField("age"); System.out.println(age); /*Field[] getDeclaredFields() 返回 Field 对象的一个数组,获取所有的字段对象。 */ Field[] declaredFields = clazz.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); }
2.6通过反射获取类的方法:
例子
public static void main(String[] args) throws Exception{ /*通过反射技术获取自定义方法对象 * */ //获取字节码对象 Class clazz = Class.forName("com.bianyiit.fanshe.Student"); /* Method getDeclaredMethod(String name, Class<?>... parameterTypes) 返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法。*/ /* Method run = clazz.getDeclaredMethod("run"); System.out.println(run); Method sleep = clazz.getDeclaredMethod("sleep", String.class, Integer.class); System.out.println(sleep); */ /* Method[] getDeclaredMethods() 返回 Method 对象的一个数组,获取的是本类中所有的方法对象,不包含继承类的方法 */ Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); }
反射总结:
1.获取类的字节码对象
2.通过类的字节码对象获取该类的(有参或者无参)构造方法
3.通过获取的构造方法创建该类的具体实例对象
4.可以通过获取的实例对象可以调用该类的成员了(成员属性和成员方法)
今日学习感受:学习的路很漫长,需要自己一点一滴去积累,坚持每天学习掌握一到两个知识点,经过日积月累,每天朝着自己的小目标努力靠近,实现自己的一个个目标,小计划,最后我相信我可以完成自己的梦想。最后一句话:人的价值是由自己决定的.,坚持学习,保持学习的态度,努力,你终会越来越幸运!