Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WindowListener操作关闭/打开_Java_File_Exception_Stream_Listener - Fatal编程技术网

Java WindowListener操作关闭/打开

Java WindowListener操作关闭/打开,java,file,exception,stream,listener,Java,File,Exception,Stream,Listener,因此,我试图在用户关闭应用程序时保存应用程序的当前状态,当应用程序重新打开时,从保存在驱动器上的序列化文件读取数据 我必须以某种方式检查目录中是否有这样的文件,如果有,从中读取,如果没有(FileNotFOundException),应用程序通常应该在没有任何数据的情况下启动,然后保存它(在这种情况下,我会手动删除该文件) 这是我定义的侦听器类(现在我删除了文件,它抛出filenotfound): 类ShraniPovrni实现WindowListener{ 私人ArrayList seznam

因此,我试图在用户关闭应用程序时保存应用程序的当前状态,当应用程序重新打开时,从保存在驱动器上的序列化文件读取数据

我必须以某种方式检查目录中是否有这样的文件,如果有,从中读取,如果没有(FileNotFOundException),应用程序通常应该在没有任何数据的情况下启动,然后保存它(在这种情况下,我会手动删除该文件)

这是我定义的侦听器类(现在我删除了文件,它抛出filenotfound):

类ShraniPovrni实现WindowListener{
私人ArrayList seznamLadij;
公共ShraniPovrni(ArrayList seznamLadij){
this.seznamLadij=seznamLadij;
}
@凌驾
公共无效窗口关闭(WindowEvent e){
试一试{
ObjectOutputStream oos=新的ObjectOutputStream(新的GziOutputStream(新的FileOutputStream(“Janezek.ser”));
oos.writeObject(seznamLadij);
oos.flush();
oos.close();
}捕获(例外情况除外){
System.out.println(“Tezave”+ex.toString()+ex.getMessage());
例如printStackTrace();
}
}
@凌驾
公共无效窗口已激活(WindowEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效窗口关闭(WindowEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效窗口已停用(WindowEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效窗口取消确认(WindowEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效窗口图标化(WindowEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效窗口已打开(WindowEvent e){
试一试{
ObjectInputStream oos=新的ObjectInputStream(新的GZIPInputStream(新的FileInputStream(“Janezek.ser”));
seznamLadij=(ArrayList)oos.readObject();
oos.close();
System.out.println(seznamLadij);
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
更改

public void windowOpened(WindowEvent e) {
    try {
        ObjectInputStream oos = new ObjectInputStream(new GZIPInputStream(new FileInputStream("Janezek.ser")));
        seznamLadij = (ArrayList<TovornaLadja>) oos.readObject();
        oos.close();
        System.out.println(seznamLadij);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
public void windowOpened(WindowEvent e){
试一试{
ObjectInputStream oos=新的ObjectInputStream(新的GZIPInputStream(新的FileInputStream(“Janezek.ser”));
seznamLadij=(ArrayList)oos.readObject();
oos.close();
System.out.println(seznamLadij);
}捕获(例外情况除外){
例如printStackTrace();
}
}

public void windowOpened(WindowEvent e){
试一试{
如果(新文件(“Janezek.ser”).exists()){
ObjectInputStream oos=新的ObjectInputStream(新的GZIPInputStream(新的FileInputStream(“Janezek.ser”));
seznamLadij=(ArrayList)oos.readObject();
oos.close();
System.out.println(seznamLadij);
}
否则{
//没有文件,请重新开始
}
}捕获(例外情况除外){
例如printStackTrace();
}
}

那么你到底想问什么?我如何检查文件是否存在,然后从中读取,如果不存在,我如何使它不捕获异常,而只是忽略它并“重新开始”,然后创建新文件
public void windowOpened(WindowEvent e) {
    try {
        ObjectInputStream oos = new ObjectInputStream(new GZIPInputStream(new FileInputStream("Janezek.ser")));
        seznamLadij = (ArrayList<TovornaLadja>) oos.readObject();
        oos.close();
        System.out.println(seznamLadij);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
public void windowOpened(WindowEvent e) {
    try {
        if(new File("Janezek.ser").exists()) {
            ObjectInputStream oos = new ObjectInputStream(new GZIPInputStream(new FileInputStream("Janezek.ser")));
            seznamLadij = (ArrayList<TovornaLadja>) oos.readObject();
            oos.close();
            System.out.println(seznamLadij);
        }
        else {
            //No file is there, start fresh
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}