Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:53:24+00:00 2026-06-16T06:53:24+00:00

I am copying directories and files from one windows server to another windows server

  • 0

I am copying directories and files from one windows server to another windows server using Swing and that works fine. I want to have a Joption Messagedialog pop up when the windows server goes down unexpectedly while copying so gave it in the catch block but it never dispays the pop up when the server goes down(I restart the windows server manually while copying but cannot see the pop up). Can someone help and here is the code

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:53:25+00:00Added an answer on June 16, 2026 at 6:53 am

    Your try-catch is a little akward.

    You try and close the streams on the event of an exception AND within the finally block.

    Finally is guaranteed to be called regardless, so you can save yourself some code by using it to close of the steams..

    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) {
        }
    
    }
    

    It appears that your code is using a SwingWorker, but you call textarea.append(" Done!\n") within it. This is VERY, VERY bad.

    Your process method needs to be capable of doing this…Basically when you process receives 100, it should be capable of updating the text area.

    You could also allow the exception to handled else where, allowing the doInBackground method to throw the exception. This would allow you to use the done method and the get method to determine if an exception has occurred, the added benefit of which is done is called within the EDT

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble copying a row from one table to another using sqlite3 in
I have this PHP code to copy files from one directory to another and
I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in
I'm using Python 2.6 and PyGTK 2.22.6 from the all-in-one installer on Windows XP,
I have a subversion repo with directories that contain the usual source-controlled files, but
I'm trying to copy multiple files from a deep source tree that have the
I have a few directories that contain a lot of files. As some of
I'm using Apache commons IO for copying files and Directories and I need to
I have an application that is looking through some files for old data. In
Python seems to have functions for copying files (e.g. shutil.copy ) and functions for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.