This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
How do I read all the files in a folder from Java?
== So far, I’ve used operators to compare all strings in my program. However, I encountered an error,.equals() instead changed one of them to an error and fixed it.
= = is bad? When should you use it and when shouldn’t you use it? What’s the difference?
Answer:
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);
Copy the code
The Files.walk API is available from Java 8.
try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) {
paths
.filter(Files::isRegularFile)
.forEach(System.out::println);
}
Copy the code
This example uses the try-with-Resources pattern recommended in the API guide. It ensures that the stream will be closed in any case.
Answer:
File folder = new File("/Users/you/folder/"); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { if (file.isFile()) { System.out.println(file.getName()); }}Copy the code
The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…
The authors suggest that a simple way to do this is fine
I looked at the mmap + FileChannel test and found it to be even better.
As you can see, The Performance of FileChannel is relatively high.
Briefly, take rocketMQ, which is a popular file system for storing data, producing and consuming data as directly manipulated files, It involves page caching, FileChannel, and FileChannel reading a page of 4KB at a time, thanks to ByteBuffer buffers and MMAP memory mapping
RokcetMQ has also been tuned for better performance
Preallocate MappedFile mlock System call file preheat sequential read and sequential writeCopy the code
I’m just doing a piece here, everybody come on!
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️