0

I am super confused on how to make a program that scans a directory for files (and files in sub directories) for information such as size, name, path, etc. For now, I just need this information written to a txt file, but I will later need to use this txt file to copy all my files to a directory on a remote computer.

I’m not very experienced with Java so if there are some libraries that you think I should research, please let me know. I think the scariest part of getting this information is how to tell computer that you need to enter a sub directory to search for its contents (like going into a Documents folder from Home, and then maybe even going into an EnglishClass folder from there).

I hope this isn’t too vague. Let me know if you need more details.

FrankM
  • 1
  • 1
  • It actually is pretty vague. Questions like these are best presented with some (nearly) working code. We cannot write a complete programme for you and there are possibly many approaches and there is no single "best library" for such tasks. It all depends on context. – planetmaker Oct 15 '18 at 12:49
  • https://stackoverflow.com/questions/5694385/getting-the-filenames-of-all-files-in-a-folder (and there's probably lots of others). – Steve Smith Oct 15 '18 at 13:09
  • @planetmaker I wasn’t asking for the “best library” and I’m not asking you to write my program. I was wondering, research-wise, where is a good place to start in your opinion? Thanks for your advice, I’ll try to be more specific next time. – FrankM Oct 15 '18 at 13:25

1 Answers1

0

This will print a list of files from the current directory and their sizes:

List<String> fileList = Arrays.asList(new File(".").list());
fileList
    .stream()                   // Use java streaming api.
    .map(f -> new File(f))      // Convert file name into file.
    .forEach(f -> {             // Iterate over each file.
        // Print info about each file.
        System.out.println("File name:"+f.getAbsolutePath());
        System.out.println("File size:"+f.length());
    }
);

If you want to scan sub-directories, read up on how to use recursion. (Hint: f.isDirectory() {...} )

BrentR
  • 878
  • 6
  • 20