Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- class FileNotDirectoryException extends Exception {
- public FileNotDirectoryException(String message) {
- super(message);
- }
- }
- public class Main {
- public static void main(String[] args) {
- File[] files = (getFilesInFolder("/home/alek/Desktop/"));
- for (File f : files) {
- System.out.println(f.getName());
- }
- }
- public static File[] getFilesInFolder(String path) {
- try {
- File f = new File(path);
- if (!f.exists()) {
- throw new FileNotFoundException("The file \"" + path + "\" doesn't exist.");
- }
- if (!f.isDirectory()) {
- throw new FileNotDirectoryException("The file \"" + path + "\" isn't a directory.");
- }
- return f.listFiles();
- } catch (FileNotFoundException | FileNotDirectoryException e) {
- System.err.println(e.getMessage());
- }
- return new File[0]; // In case of any errors, an empty list is returned
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement