0

I am trying to access a file in remote shared location.

////hostname//data//upload//123//test.txt
File sourceFile=new File("////hostname//data//upload//123//test.txt");
sysout("sourceFile.exists()"+sourceFile.exists())//returning false

If a directory is empty file.exists() is returning true. I am using Java 1.6

I don't understand what is this weird behavior.

1
  • Are you sure you need to write // and not \\ ? Commented Jul 3, 2015 at 12:02

3 Answers 3

1

First of all to come back to Erwin´s suggestion, this is not the right attempt. The character \ is used in Java as an escape sequence, usually to print out reserved characters. For example will

String s = "The weather is really "nice" today";

result in an error, as " is already reserved for strings. The correct version would be

String s = "The weather is really \"nice\" today";

Coming back to the question, you have to know that when you create a file and test if it exists Java will validate the abstract pathname of the file. That said, if your abstact path is a directory and it exists true will be returned.

Edit:
If you intend to check if an abstract pathname is a directory try the following:

// Check if a file is a directory
if(file.isDirectory()) {

}

// Check if a file contains something
if(file.list().length > 0) {

}
Sign up to request clarification or add additional context in comments.

1 Comment

Issue is fixed, as the application windows service is not running on the correct user. So, it is throwing this exception due to lack of permissions.Thanks for the help guys.
0

Check this example ,it checks the directory else creates a new one then your new file created.

File f = new File("D:/image_send");
    File file = new File("D:/image_send/" + date + ".txt");
    try {
        if(!f.isDirectory()){
        f.mkdirs();
        }
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("File created Success");

Comments

0
public static boolean fileTransfer(String src, String des) throws Exception {
    if (des == null || des.equals("") || src == null || src.equals("")) {
        return false;
    }
    File fileExisting = new File(src);
     File fileNew = new File(des+ fileExisting.getName());
    if (fileExisting.exists() && !fileExisting.isDirectory()) {
        if (fileExisting.renameTo(fileNew)) {
            System.out.println("File is moved successful!");
        } else {
            System.out.println("File is failed to move!");
        }

    }
    return fileNew.exists();
}

This is the code for file transfer as per your comment ,use src as sourcepath and des as destination path if you get a boolean false,that means path given is wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.