简体   繁体   English

如何使用 Java 8 在 Spring Boot 中获取目录(不是文件)的完整系统路径

[英]How to get the full system path of a directory (not file) in Spring boot with Java 8

I am coding in Spring Boot 1.5 with Java 8 I have a directory on this path :我正在使用 Java 8 在 Spring Boot 1.5 中编码我在此路径上有一个目录:

  C:\Users\me\IdeaProjects\MyApp\server\src\main\resources\static\documents\1\folder_a23

I don't know how i can get with a Java function this path as a String/File/Path/Stream.我不知道如何使用 Java 函数将此路径作为字符串/文件/路径/流。 Is there a way to get this path as result of a search of "/folder_a23" ?有没有办法通过搜索“/folder_a23”来获得这条路径? Thank you very much for every answer!非常感谢您的每一个回答!

The File Object handles everything you can do with a File on the File system.文件对象处理您可以对文件系统上的文件执行的所有操作。 Even though the name does not properly suggest it, a File object is an abstract representation of file OR directory pathnames.尽管名称没有正确暗示它,但File对象是文件或目录路径名的抽象表示。

Have a look at #getAbsolutePath()看看#getAbsolutePath()

File file = new File(".....");
String absolutePath = file.getAbsolutePath();

This giles you the absolut path of the File.这为您提供了文件的绝对路径。

If the String you used to create the File object points to a directory, you can get the content of this directory with .listFiles();如果用于创建 File 对象的 String 指向一个目录,则可以使用.listFiles();获取该目录的内容.listFiles();

This allows you to loop through the content of a directory and perform the checks you need.这允许您遍历目录的内容并执行所需的检查。

file = new File("c:/test");
File[] paths = f.listFiles();
for(File path: paths) {
    // perform your checks here
}

This function is not recursive by nature.这个函数本质上不是递归的。 If you want to descend into sub directories, you will need to code that on your own.如果你想进入子目录,你需要自己编码。

However, this assumes that you are indeed working with files.但是,这假定您确实在处理文件。 If you bundle your programm as an .jar (or .war) file and run it from there, their woun't be a file on the file system as .jar are zipped java archives.如果您将程序捆绑为 .jar(或 .war)文件并从那里运行,它们将不会是文件系统上的文件,因为 .jar 是压缩的 java 档案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM