会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132432个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2836楼

为什么报错系统找不到指定的文件,代码如下

package com.jscn.test;

import com.jscn.entity.Employee;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

public class test {
    public static void main(String[] args) throws DocumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, FileNotFoundException {
        SAXReader reader =new SAXReader();
        Document doc =reader.read(new FileInputStream("com.jscn.entity/employees.xml"));
        Element root =doc.getRootElement();
        for (Iterator<Element>iterator=root.elementIterator();iterator.hasNext();){
            Element element=iterator.next();
            System.out.println("子节点"+element);
        }
        for (Iterator<Element>iterator=root.elementIterator();iterator.hasNext();){
            Class c=Class.forName("Employee");
            Constructor<Employee> constructor=c.getConstructor();
            Employee employee=constructor.newInstance();
            Element element=iterator.next();
            for (Iterator<Element>iterator1=element.elementIterator();iterator.hasNext();){
                Element element1=iterator1.next();
                String string=element1.getName();
                String string1=element1.getText();
                System.out.println(element1+"\t"+string1);
            }
        }
    }
}

我的包结构如下

56{@5S)5XIEZDJCS}]2R[{N.png

报错内容如下

W2ZO@EC$UL_]GZ8MRC0)6PR.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2839楼

package com.bjsxt.io;

import java.io.Serializable;
import java.util.ArrayList;

/** 
 * <p>Title: Class403</p>  
 * <p>Description: 自定义对象2(将对象序列化写入文件中)</p>  
 * @author xiaoding
 * @date Jun 10, 2020  
 * @version 1.0 
 */
public class Class403 implements Serializable {
    //功能属性
    private int classs;        //班级
    private ArrayList<Student> a1;  //学生
//    private int num;
    //构造方法
    Class403() {
        super();
    }
    public Class403(int classs,ArrayList<Student> a1) {
        this.classs = classs;
        this.a1 = a1;
    }
    
    //get、set方法
    public int getClasss() {
        return classs;
    }
    public void setA1(ArrayList<Student> a1) {
        this.a1 = a1;
    }
    public ArrayList<Student> getA1() {
        return a1;
    }
    public void setClasss(int classs) {
        this.classs = classs;
    }
    
    //toString方法
    @Override
    public String toString() {
        return "Class403 [classs=" + classs + ", a1=" + a1 + "]";
    }
}



package com.bjsxt.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/** 
 * <p>Title: TestClass403</p>  
 * <p>Description: 测试类2测试序列化和反序列化</p>  
 * @author xiaoding
 * @date Jun 10, 2020  
 * @version 1.0 
 */
public class TestClass403 {
    //写入对象
    public static void write() {
        //创建ArrayList对象,写入学生信息
        ArrayList<Student> a1 = new ArrayList<Student>();
        a1.add(new Student(0,"张三",20));
        a1.add(new Student(1,"李四",30));
        a1.add(new Student(2,"王五",25));
        a1.add(new Student(3,"小明",18));
        ObjectOutputStream fis = null;
        //创建ObjectOutputStream写入对象
        try {
            fis = new ObjectOutputStream(new FileOutputStream("D:/student.txt"));
            fis.writeObject(new Class403(403,a1));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭IO流
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    //读取对象
    public static void read() {
        //创建ObjectInputStream
        ObjectInputStream fis = null;
        try {
            fis = new ObjectInputStream(new FileInputStream("D:/student.txt"));
            //读取对象
            System.out.println(fis.readObject());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭IO流
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    //main方法
    public static void main(String[] args) {
        //write();
        read();
    }
}

首先第一次序列化对象,Class403的属性值只有两个,先写入在自读取

批注 2020-06-10 223855.png

第二次,更改Class403的实例属性,

//    private int num; 解除注释

输出方法还是上面图片那样,他会报错,因为版本不兼容,需要指定版本号

但是我改成这样输出就不会报错,为什么?

批注 2020-06-10 224344.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2840楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 2844楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 2847楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2848楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637