Oto kolejny przykład pomyślnego uruchomienia na platformie 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");
}
}
}
Zauważ, że 'List' istniały przed strumieniami, a także, a jednak nie dodać' asStream() 'do tego;) – Thirler
Uwaga @ odpowiedzi Holger jest poniżej - można uniknąć konwersji jeśli masz strumień bezpośrednio z' Pliki .newDirectoryStream (Path) '. –