catch块中的Joptionpane Message对话框

时间:2012-12-23 03:11:10

标签: java

我正在使用Swing将目录和文件从一个Windows服务器复制到另一个Windows服务器,并且工作正常。我希望当Windows服务器在复制时意外关闭时弹出Joption Messagingialog,因此在catch块中给出它但是当服务器关闭时它永远不会弹出窗口(我在复制时手动重启Windows服务器但是看不到弹出)。有人可以帮忙,这是代码

try {
    textarea.append("Copying " + sourceFile.getAbsolutePath()
        + "   to " + targetFile.getAbsolutePath());
    is = new BufferedInputStream(new FileInputStream(sourceFile));
    bos = new BufferedOutputStream(new FileOutputStream(targetFile));

    long fileBytes = sourceFile.length();
    long soFar = 0;

    int theByte;

    while ((theByte = bis.read()) != -1) {
        bos.write(theByte);

        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }

    bis.close();
    bos.close();
    publish(100);
    textarea.append(" Done!\n");
} catch (Exception excep) {
    task.cancel(true);
    bos.flush();
    bis.close();
    bos.close();
    jf2 = new JFrame();
    jf2.setSize(401, 401);
    jf2.setDefaultCloseOperation(jf2.EXIT_ON_CLOSE);
    JOptionPane.showMessageDialog(jf2,
        "The Server is not accessible or it may be down because of Network Issue",
        "ERROR", JOptionPane.ERROR_MESSAGE);
} finally {
    if (bis != null) {
        bis.close();
    }
    if (bos != null) {
        bos.close();
    }
}

2 个答案:

答案 0 :(得分:2)

你的尝试有点尴尬。

您尝试在finally块内发生异常事件时关闭流。

最后保证无论如何被调用,所以你可以通过使用它来关闭蒸汽来节省一些代码..

try {
    textarea.append("Copying " + sourceFile.getAbsolutePath()
                    + "   to " + targetFile.getAbsolutePath());
    is = new BufferedInputStream(new FileInputStream(sourceFile));
    bos = new BufferedOutputStream(new FileOutputStream(targetFile));

    long fileBytes = sourceFile.length();
    long soFar = 0;

    int theByte;

    while ((theByte = bis.read()) != -1) {
        bos.write(theByte);

        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }

    // Not required, finally will take care of it...
    //bis.close();
    //bos.close();
    publish(100);
    // !! THIS IS VERY, VERY NAUGHTY !!
    textarea.append(" Done!\n");
} catch (Exception excep) {

    JOptionPane.showMessageDialog(null, "The Server is not accessible or it may be down because of Network Issue", "ERROR", JOptionPane.ERROR_MESSAGE);
    task.cancel(true);

} finally {

    try {
        // techniqually, this gets taken care of when you close the stream,
        // but I tend not to trust it either...
        bos.flush();
    } catch (Exception e) {
    }

    try {
        bis.close();
    } catch (Exception e) {
    }
    try {
        bos.close();
    } catch (Exception e) {
    }

}

您的代码似乎使用SwingWorker,但您在其中调用了textarea.append(" Done!\n")。这非常非常糟糕。

您的process方法需要能够执行此操作...基本上,当您process收到100时,它应该能够更新文本区域。

您还可以允许异常处理其他位置,允许doInBackground方法抛出异常。这将允许您使用done方法和get方法来确定是否发生了异常,在EDT中调用了done的额外好处

答案 1 :(得分:1)

首先,我不喜欢所有的手动资源管理,因此我将其更改为使用Java 7的try-with-resources为您执行此操作,从而允许删除finally块和所有实例close()flush()(顺便说一句,关闭调用刷新,所以你不需要两者。

其次,我不知道该消息框声明是否有效,JOptionPane.showMessageDialog()的Javadoc表示第一个参数应该是Component parentComponent,但是你要声明一个新的不可见JFrame而不是,因此,如果您发布的此代码位于JFrame类中,请传入this。完整的,我会给它一个:

try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)))
{
    textarea.append("Copying " + sourceFile.getAbsolutePath() +
                    " to " + targetFile.getAbsolutePath());
    long fileBytes = sourceFile.length();
    long soFar = 0;
    int theByte;
    while((theByte = bis.read()) != -1)
    {
        bos.write(theByte);
        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }
    publish(100);
    textarea.append(" Done!\n");
}
catch(Exception excep)
{
    task.cancel(true);
    JOptionPane.showMessageDialog(this, "The Server is not accessible or it may be down because of Network Issue", "ERROR", JOptionPane.ERROR_MESSAGE);
}