[使用Java检查文件夹在datalake中是否存在

问题描述 投票:0回答:1

我想检查数据湖中是否存在文件夹。如果存在,请在其中创建一个文件,如果不存在,请创建一个文件夹,然后在该文件夹内创建一个文件

    File directory = new File("/Raw/TEST_1/test");
    System.out.println("check if directory exist");
    if (directory.exists() == false) {

        client.createDirectory("/Raw/TEST_1/test");

        System.out.println("Directory created.");

        OutputStream stream = client.createFile("/Raw/TEST_1/test/" + FuFileName, IfExists.OVERWRITE);

    } else {
        System.out.println("Directory exist.");
        OutputStream stream = client.createFile("/Raw/TEST_1/test" + FuFileName, IfExists.OVERWRITE);
    }

} catch (ADLException ex) {
    printExceptionDetails(ex);
} catch (Exception ex) {
    System.out.format(" Exception: %s%n Message: %s%n", ex.getClass().getName(), ex.getMessage());
}

每一次directory.exists()给我输出false,即使该文件夹存在,如果不给Directory.exists()output作为true并且不执行else语句也是如此

java azure-data-lake
1个回答
0
投票

如果使用的是Java 7或更高版本,则建议使用java.nio来访问文件系统。

这个非常简单的示例可以告诉您给定的路径是否存在,以及它是否是目录或其他内容:

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

...

public static void main(String[] args) {
    Path dirPath = Paths.get("/Raw/TEST_1/test");

    if (Files.exists(dirPath)) {
        System.out.println(dirPath.toAbsolutePath().toString() 
                + " exists on the file system");
        if (Files.isDirectory(dirPath)) {
            System.out.println(dirPath.toAbsolutePath().toString() 
                    + " is a directory");
        } else {
            System.err.println(dirPath.toAbsolutePath().toString()
                    + " is not a directory");
        }
    } else {
        System.err.println(dirPath.toAbsolutePath().toString()
                + " does not exist on the file system");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.