有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Ubuntu 14.04上文件夹存在的目录Java测试

我有一个文件夹:

/home/plattens/workspace/jguids30ma/symbols

我试图测试它在Java中的存在性:

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

我尝试了各种选择,但到目前为止,每一个变体都失败了,目录测试失败,路径无效

文件夹已存在


共 (3) 个答案

  1. # 1 楼答案

    我还用你的代码在Ubuntu-14.04上进行了测试,结果是真的

    所以我认为你的文件夹可能有一些问题

    我想你需要检查一下“ls-la”命令, 和“d_____________;”特别是其他用户的意见

    我希望你发现什么不对劲

  2. # 2 楼答案

    我测试了你的代码并编译了。我得到了正确的输出,所以您可以引用此代码并删除不必要的导入包

    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");
            }
        }
    }
    

    我希望它能帮助你

  3. # 3 楼答案

    这很奇怪,只是将代码修改为:

    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;                                                            
    }
    

    使用文本路径进行的所有单独检查都有效,但是最后一次测试仍然失败,结果证明参数“arystratr”在路径周围实际上有引号