-1

I have a folder:

/home/plattens/workspace/jguids30ma/symbols

I am trying to test for its existance in Java:

Path objSymbolPath = Paths.get("/home/plattens/workspace/jguids30ma/symbols");
if ( Files.isDirectory(objSymbolPath) != true ) {
    System.out.println("Path is not valid");
}

I've tried various options, but so far every variation fails, the test for the directory fails and 'Path is not valid'.

The folder exists.

7
  • Try this if (Files.exists(objSymbolPath)) {sop("exist")}else{sop("not exist")} Feb 12, 2016 at 10:45
  • I have tried that, it also fails, I think it may have something to do with the location being in my user space.
    – SPlatten
    Feb 12, 2016 at 10:47
  • Did you check import package because i tried this code it is working for me i import java.nio.file.Path Feb 12, 2016 at 10:52
  • The packages I have imported are: import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;
    – SPlatten
    Feb 12, 2016 at 10:55
  • Did you try creating a folder in your home directory and testing for this?
    – SPlatten
    Feb 12, 2016 at 11:02

3 Answers 3

0

I also tested with your code on Ubuntu-14.04 and I got true.

So I think your folder may have some problem.

I think you need to check with "ls -la" command, and "d_______r__" especially r opiton for otherUser..

I hope you find something wrong.

2
  • Result of "ls -la": "drwxrwxr-x 2 plattens plattens 4096 Feb 12 10:45 symbols"
    – SPlatten
    Feb 12, 2016 at 10:50
  • Doesn't it working for the simple folder like /home ?
    – Gary Y Kim
    Feb 12, 2016 at 11:29
0

I tested your code and compiled. I'm getting correct output so you can refer this code and remove unnecessary import package.

package test;

import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Test {
    public static void main(String[] args) {
        Path objSymbolPath = Paths.get("/home/admin/Desktop/testing");
        if (Files.isDirectory(objSymbolPath) != true) {
            System.out.println("Path is not valid");
        } else {
            System.out.println("Path is valid");
        }
    }
}

I hope it will help you.

0

Its very odd, just modified the code to this:

objSymbolPath = Paths.get(arystrAttr[1]);

if ( clsFolderTest.blnExists(Paths.get("/home/")) != true ) {
    System.out.println("doesn't exist!");
}
if ( clsFolderTest.blnExists(Paths.get("/home/plattens/")) != true ) {
    System.out.println("doesn't exist!");
}
if ( clsFolderTest.blnExists(Paths.get("/home/plattens/workspace/")) != true ) {
    System.out.println("doesn't exist!");
}
if ( clsFolderTest.blnExists(Paths.get("/home/plattens/workspace/jguids30ma/")) != true ) {
    System.out.println("doesn't exist!");
}
if ( clsFolderTest.blnExists(Paths.get("/home/plattens/workspace/jguids30ma/symbols/")) != true ) {
    System.out.println("doesn't exist!");
}
if ( clsFolderTest.blnExists(objSymbolPath) != true ) {
    clsMain.errMessage(clsSymbol.class.toString() + ":alstrToSVG"
                      ,"Symbol path does not exists or is not a folder!");
    return null;                                                            
}

All the individual checks with the literal paths work, however the last test still fails, in the end it turns out the the parameter 'arystrAttr' actually had quotes around the path.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.