I am want to list out the only file names from the folder in Java 8. I have tried this code, but it is giving me the complete path.
try {
List<java.nio.file.Path> files = Files.list(new File("F://csv/").toPath())
.filter(p -> !p.getFileName()
.toString().startsWith("."))
.limit(3)
.collect(Collectors.toList());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use Path::getFileName to get the file name from a path:
import static java.util.stream.Collectors.toList;
List<Path> fileNames = Files.list(Paths.get("f:/csv"))
.filter(...)
.limit(...)
.map(Path::getFileName)
.collect(toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With