How to search all the local disks and list all the files present in the disk? how to detect the removable disk, so that i can search removable disks also. i will be helpfull if i can get a code snippet. thank u in advance.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
How to search all the local disks and list all the files present in the disk? how to detect the removable disk, so that i can search removable disks also. i will be helpfull if i can get a code snippet. thank u in advance.
There are several reasons why you may not be able to do this:
protected locations, and the many different ways different OS's organize drives. Protected locations are impossible to access without hacking (possible legal issues here) or having all the passwords/access rights. OS files as well as many Virus Protection Software files have vastly restricted access, as well as any files that a certain user wishes to mark as protected.
I have a method for this, however it will take you a very long time depending on the number of files you have on your disk.
WARNING: Use with caution
public static List<File> listFiles(final String path, final boolean recursive) { final List<File> files = new ArrayList<File>(); final File startPath = new File(path); if (startPath != null && startPath.listFiles() != null) { for (final File file : startPath.listFiles()) { if (file.isDirectory() && recursive) { files.addAll(listFiles(file.getAbsolutePath(), recursive)); } else if (file.isFile()) { files.add(file); } } } return files; }
And to invoke this with your drive root, call it like this.
final List<File> files = listFiles(File.separator, true);
// Json
The first code block is the actual method which will call itself, the second code block is the line of code which will start the process off.
// Json