Reading files in a folder java


















Test, Test1 and Test2 are directories here and then you have files with in those directories. There are two ways to list all the files in a folder; one is using the listFiles method of the File class which is there in Java from 1.

Another way to list all the files in a folder is to use Files. Here we have two overloaded listAllFiles methods. First one takes File instance as argument and use that instance to read files using the File. In that method while going through the list of files under a folder you check if the next element of the list is a file or a folder. If it is a folder then you recursively call the listAllFiles method with that folder name.

If it is a file you call the readContent method to read the file using BufferedReader. Another version of listAllFiles method takes String as argument. In Java 7 and higher you can use listdir. You can also create a filter that can then be passed into the newDirectoryStream method above. For other filtering examples, [see documentation].

If you want more options, you can use this function which aims to populate an arraylist of files present in a folder. Options are : recursivility and pattern to match. Simple example that works with Java 1. I'd like to add that it is possible to use relative path as-well. Java 8 Files. Just to expand on the accepted answer I store the filenames to an ArrayList instead of just dumping them to System.

To see what is available at runtime. Now to get the files from a specific folder, let's say you have a folder called 'res' in your resources folder, just replace:. You can put the file path to argument and create a list with all the filepaths and not put it the list manually. Then use a for loop and a reader. Example for txt files:. We can use org. FileUtils, use listFiles mehtod to read all the files in a given folder. This read all the files in the given directory with given extensions, we can pass multiple extensions in the array and read recursively within the folder true parameter.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to read all files in a folder from Java? Ask Question. Asked 12 years, 1 month ago.

Active 3 days ago. Viewed 1. Line 1, 3 3 gold badges 14 14 silver badges 37 37 bronze badges. I agree, point out the class to use so the poster can become familiar with the various method, otherwise the poster doesn't bother to read the API to find out what other methods are available.

Did you mean all files in a folder, or all files in a folder and all subfolders? An up to date link to the API: docs. If you are using Java 7 or newer you can use Files. Add a comment. Active Oldest Votes. If you plan to use this information to find the files, you may find the path given by getPath to be more useful.

Can I use this method to find all files of a specific type say pdf or html across my whole system? My concern is efficiency, is it fast enough to be used for systems with thousands of files or is there a better alternative?

It's fast, efficient, and readable enough to get your job done. In Java 8 you can also make use of the filter method. Then forEach is not longer needed. Just to indicate it's only initialised once. Show 4 more comments. David Robles David Robles 9, 8 8 gold badges 36 36 silver badges 46 46 bronze badges.

I think this will print the directory names, and no filenames — Fortega. By far easier to understand than accepted answer. If the given directory itself contains other directories, their content won't be read — Farid. Paths; import java. DirectoryIteratorException; import java. DirectoryStream; import java. FileVisitResult; import java.

SimpleFileVisitor; import java. DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.



0コメント

  • 1000 / 1000