Make a data structure and implement an algorithm to print all the files in a directory. (The root directory can have sub-directories too.)



Make a data structure and implement an algorithm to print all the files in a directory. (The root di..

Answer / Sumit Kumar Yadav

To list all files in a directory using Java's File class, you can use a recursive approach. Here's an implementation:n```npublic static void listFiles(File dir) {n if (dir.isDirectory()) {n File[] children = dir.listFiles();n for (File file : children) {n if (file.isDirectory()) {n System.out.println("Directory: " + file.getName());n listFiles(file);n } else {n System.out.println("File: " + file.getName());n }n }n }n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is finally and finalize in java?

1 Answers  


Why are the objects immutable in java?

1 Answers  


What is a final class ?

1 Answers  


What is data type modifier?

1 Answers  


What is the static field modifier?

1 Answers  


What is a heavyweight component?

1 Answers  


How do you write a scanner class in java?

1 Answers  


what is webservices

5 Answers   Consultancy, Mind Tree,


What is instance example?

1 Answers  


If try block is successfully executed, Then Is Finally block executed?

1 Answers   PUCIT,


Why we use set in java?

1 Answers  


Is alive and join method in java?

1 Answers  


Categories