简体   繁体   English

仅用于目录路径的URI?

[英]URI for a directory path only?

Is it possible to define a URI representing a directory path only (ie with no actual file terminating the URI)? 是否可以定义仅表示目录路径的URI(即,没有实际的文件终止URI)? Example: file:///path/to/files . 例如: file:///path/to/files

I have the need to specify paths to directories to a Java program and would like to specify them as a URI rather than just a String directory path. 我需要指定Java程序目录的路径,并希望将它们指定为URI,而不仅仅是String目录路径。 The program receiving this information would treat the URI as a directory specifier and expect to read whatever files are contained within. 收到此信息的程序会将URI视为目录说明符,并希望读取其中包含的所有文件。 It does not care what the file names are. 它不在乎文件名是什么。

UPDATE: 更新:

If this is allowed, would I be able to "open" the URI in order to get the listing of files within, and then be able to open them for reading? 如果允许这样做,我是否可以“打开” URI以获取其中的文件列表,然后可以打开它们进行读取?

Yes it is possible. 对的,这是可能的。 here's an exemple lets say myfolder is a folder that contains 3 files (file1.txt, file2.txt, file3.txt). 这是一个示例,可以说myfolder是一个包含3个文件(file1.txt,file2.txt,file3.txt)的文件夹。

URI uri = new URI("file:///c:/myfolder/");
File folder =new File(uri);
for(File file : folder.listFiles()){
System.out.println(file.getName()); 
}

the output will be : 输出将是:

file1.txt
file2.txt
file3.txt

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

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