Java Read all files in drive C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java Read all files in drive C

how to read all files stocked in drive c with java

8th Mar 2020, 5:16 PM
Compte Mail
Compte Mail - avatar
5 Answers
+ 6
Below example shows how to get list of all file objects from the given folder. First create File object by passing folder path. Call listFiles() method on file object to get list of file names in the given folder. package com.java2novice.files;   import java.io.File;   public class FilesListFromFolder {           public static void main(String a[]){         File file = new File("C:/MyFolder/");         File[] files = file.listFiles();         for(File f: files){             System.out.println(f.getName());         }     } }
8th Mar 2020, 5:29 PM
Bruno Félix
Bruno Félix - avatar
0
thank you but this read only specific folder , I wish read folder and sub folders
9th Mar 2020, 1:39 AM
Compte Mail
Compte Mail - avatar
0
What you intend to do by reading all files in each directories?
9th Mar 2020, 1:48 AM
Ipang
0
for tutorial
9th Mar 2020, 1:51 AM
Compte Mail
Compte Mail - avatar
0
There are factors to consider, for example, file system objects (files, directories) in modern O/Ses may be encrypted or compressed by system to prevent unauthorized access. And next, there are antivirus programs, which may presume your program was doing some aggresive action (read all files) and may take actions in attempt to keep safe the system. I'm not trying to stop you in your way, just giving you a little points to consider. If you can get Bruno's code to run, then you can turn it into a recursive function, which you can try as a test. Anyways, good luck buddy! 👍
9th Mar 2020, 2:01 AM
Ipang