Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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保存函数不支持';行不通_Java_Dialog_Save_Ioexception - Fatal编程技术网

Java保存函数不支持';行不通

Java保存函数不支持';行不通,java,dialog,save,ioexception,Java,Dialog,Save,Ioexception,嘿,我有这段代码,它应该保存一个自定义可序列化类的java.util.Vector: if(filename.equals("")){ javax.swing.JFileChooser fc = new javax.swing.JFileChooser(); if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){ filename = fc.getSelectedFile().

嘿,我有这段代码,它应该保存一个自定义可序列化类的
java.util.Vector

if(filename.equals("")){
    javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
    if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){
        filename = fc.getSelectedFile().toString();
    }
}
try{
    java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
    oos.writeObject((Object)tl.entities);
    baos.writeTo(fos);
    oos.close();
    fos.close();
    baos.close();
}catch(java.io.FileNotFoundException e){
    javax.swing.JOptionPane.showMessageDialog(this, "FileNotFoundException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}catch(java.io.IOException e){
    javax.swing.JOptionPane.showMessageDialog(this, "IOException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}

但在保存时,它会显示一个已定义的对话框错误,如:
IOException:无法保存文件:null(com.sun.java.swing.plaf.windows.WindowsFileChooserUI)
,命令行中的
javax.swing.plaf.basic.BasicListUI.convertModelToRow(BasicListUI.java:1251)中有一个
NullPointerException

也许您可以更好地检查文件名:

if (filename == null || "".equals(filename)){
    javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
    if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){
        filename = fc.getSelectedFile().toString();
    }
    if (filename == null || "".equals(filename)) {
        // Display a message or anything else
        return;
    }
}
try {
 ...
 }

我不知道到底是什么问题,因为您的异常消息不太清楚。但我对您的代码有两点意见:

  • 可以对文件名(或文件)进行空检查(如建议的)
  • 为什么要将文件更改为其文件名?保留文件对象并将其传递给流

  • 您的代码中有一些错误

    • 您可以有条件地设置文件名。如果你不设置它,你仍然会尝试使用它
    • 关闭ByteArrayOutputStream(这是无用的,请参见api)
    • 将文件对象转换回文件名,同时可以使用文件对象写入流
    我建议这样编码:

    while(file == null) { // force a file to be choosen
        javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
        if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile()
        }
        else {
            javax.swing.JOptionPane.showMessageDialog(this, "No file selected", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }
    
    try{
            java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
            java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
    
            oos.writeObject((Object)tl.entities);
            baos.writeTo(fos);
            oos.close();
            fos.close();
    
    }catch(java.io.FileNotFoundException e){
            javax.swing.JOptionPane.showMessageDialog(this, "FileNotFoundException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
    }catch(java.io.IOException e){
            javax.swing.JOptionPane.showMessageDialog(this, "IOException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
    

    您的问题很难诊断,因为您没有显示正在捕获的异常的堆栈跟踪


    为什么要通过tearrayoutputstream将某些内容序列化为
    而只将字节数组写入文件?您需要出于某种原因浪费内存吗?

    我发现了错误,保存对话框脚本本身工作正常,但向量中的类有一个空指针,导致了错误

    但是感谢所有的回复,我可以使用其中的一些:)