package com.logicbig.example.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ListExample {
public static void main(String... args) throws IOException {
String pathString = System.getProperty("java.io.tmpdir");
Path path = Paths.get(pathString);
Stream<Path> list = Files.list(path);
list.limit(5).forEach(System.out::println);
}
}
Output
C:\Users\Joe\AppData\Local\Temp\+~JF1283084198655582245.tmp
C:\Users\Joe\AppData\Local\Temp\+~JF192051292487094235.tmp
C:\Users\Joe\AppData\Local\Temp\+~JF2519104209326462184.tmp
C:\Users\Joe\AppData\Local\Temp\+~JF33147415601443728.tmp
C:\Users\Joe\AppData\Local\Temp\+~JF3396327083016795947.tmp
package com.logicbig.example.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ListExample2 {
static boolean flg = false;
public static void main(String... args) throws IOException {
String pathString = System.getProperty("java.io.tmpdir");
Path path = Paths.get(pathString);
Stream<Path> list = Files.list(path);
list.filter(p -> {
if (flg && Files.isDirectory(p) || !flg && !Files.isDirectory(p)) {
flg = !flg;
return true;
}
return false;
}).limit(5).forEach(ListExample2::printPathInfo);
}
private static void printPathInfo(Path path) {
System.out.printf("Path: %s, isDir: %s%n", path,
Files.isDirectory(path));
}
}
Output
Path: C:\Users\Joe\AppData\Local\Temp\+~JF1283084198655582245.tmp, isDir: false
Path: C:\Users\Joe\AppData\Local\Temp\48B8E8A6-40E7-4235-937D-A07C61BFF851, isDir: true
Path: C:\Users\Joe\AppData\Local\Temp\9102561600275231376, isDir: false
Path: C:\Users\Joe\AppData\Local\Temp\a03bc02c-0894-482c-a2e8-bd298cee1cb6, isDir: true
Path: C:\Users\Joe\AppData\Local\Temp\adb.log, isDir: false