How to get contents of a folder by using Java

In this tutorial I will explain about how you can display contents of a folder by using the Java language. I have created also a video where you can see the results.

I have created a folder called new, and I have writed this code to open my folder:

File dir = new File("new");

Then, I have used this code File[] list = dir.listFiles(); to list all contents from my folder new.

Next, I have used For to display contents:

for(File file : list)
{
if(file.isDirectory())
{
System.out.println("Directory: "+file.getName());
}
else
{
System.out.println("File: "+file.getName());
}
}

Video tutorial:









Leave a Comment