Dưới đây là một ví dụ khác chạy thành công trên nền tảng Windows.
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
//Using compiler java ver 1.7
class A {
public static void main(String[] args) throws Exception
{
//Tested on dos directory system
Path dir = Paths.get("c:\\a\\b\\");
/**
*
* This try with exceptions block attempts to open the specified
* directory.
*
*/
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir))
{
// This enhanced for loop block iterates through the specified
// directory and outputs each filename
for(Path p: stream)
{
System.out.println(p.getFileName());
}
}
catch(Exception e)
{
System.out.println("problems locating directory");
}
}
}
Nguồn
2017-03-16 10:31:17
Lưu ý rằng 'List' tồn tại trước suối là tốt, tuy nhiên họ đã thêm 'asStream()' để đó;) – Thirler
Note @ câu trả lời Holger của dưới đây - bạn có thể tránh chuyển đổi nếu bạn nhận được một dòng suối trực tiếp với 'tập tin .newDirectoryStream (Đường dẫn) '. –