Hello. I have a folder that contains a lot of large files, the names of which I do not know. I know only the relative path to the folder in which they are located. Without needing to download these files into my program, I need to check, say, for example, a month, and delete if yes.

Here I googled a way to find out the date the file was created along its path.

BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class); System.out.println(" creationTime: " + attr.creationTime()); 

And how to get the name of all the paths to files? After all, as I understand

 File myFolder = new File("путь к папке"); File[] files = myFolder.listFiles(); 

It loads the files themselves into the program, which I do not need because There are many files and they are large.

  • You can do this: String list [] = new File ("."). List (); - Hermann Zheboldov
  • no, it does not load, the File object (and its analogue from the new api Path ) is just an abstraction, and contains only the path. It may, for example, point to a non-existent file. Since you are using a new api, instead of File.list() use Files.list( Path ) (or Files.newDirectoryStream( Path ) if you have java 7). - zRrr

1 answer 1

Here is an example of a finished program:

 public class Dir { public static void main(String[] args) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(args[0]))) { for (Path file: stream) { if(!file.toFile().isDirectory() ) { System.out.println(file.getFileName()); } } } catch (IOException | DirectoryIteratorException x) { System.err.println(x); } } 

}

The argument is the name of the directory.