Question

In Java console application, I am trying to copy a single file from shared network, to which I have access only when connected to our institute's internal network (an internal domain).

So this obviously doesn't work when I am connected to any other network e.g. when I am working at home. Some how it tries to connect to the network drive a bit longer, then other party of my applicaiton fails.

The problem is, how do I check if this application has a valid connection to that network drive first and then try to copy this file or else let the other processes work fine.

Since, I already have a local copy of this file attach to the application. The file has to be copied only to update the one attached with application to have the latest information. However, the application works fine with this local file as well.

Ohh yes, I am looking for something that's platform independent, should work on windows, Linux and Mac.

Any directions or suggestions would be really great.

Was it helpful?

Solution

Have a look at the File class and its functions such as isDirectory() & exists(). For example,

import java.io.File;

public class Test {

    public static void main(String[] args) {
        File file = new File("\\\\server01\\\\folder");
        System.out.println(file.isDirectory());
        System.out.println(file.exists());

    }
}

For a valid UNC path, this would print:

true
true

For in invalid, i.e. unaccessible, this would print:

false
false

NB: You need the 4 slashes to represent "\" as '\' is used for escape sequences.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top