Java File Explorer program with Source Code

Let’s develop a basic file explorer using java. Java’s FILE class from package provides simple way to represent both files and directories. That means, the same class can represent both files and directories.
This program starts by creating a new file in the root. For example we can start at C drive in windows or root(/) in Linux.

File f = new File(“C:\”);  

The list of available files and folders present in the given directory can be accessed using the function listFiles(). It returns a list of files pointing to the contents inside the directory. Using a for loop buttons are created dynamically for each of these files and added to the container panel.
Handling Files
The button can be either file or folder. Using isFile() function in the FILE class, it is possible to determine whether a file is a file or directory. If it is a file, we then have to open it, if possible.
Java provides a powerful handle to the operating system using ‘Desktop’ class from java.awt package. Using Desktop we can open a file using the default program associated for it.
Open() throws an IOException and should be caught properly.
Handling Directories
When we encounter a directory, which is the false case for isFile(), we have to display the contents in that directory. What we have to do here is a simple recursive call to the display function using this path after removing the all currently present components in the container.
Container.removeAll() should do the trick.
After adding the new buttons, it is necessary to refresh the panel. Remember that the newly added components will not be visible until you refresh the panel using revalidate() and repaint() functions present in the Panel class.
Adding a back button
It is a basic requirement for File explorers to add back buttons. In order to do so, we have to remember the history of directories. A stack is used for this purpose. Whenever a directory is accessed, its parent is added to the stack. When the back button is pressed, the top of the stack is popped and that directory is displayed.
Go to directory
Again, the same display function LoadBase() is utilized here. LoadBase taked the full pathname of directory and when the user gives the directory path directly, all that we have to do is to pass the given text to LoadBase(). The path may be invalid and it is displayed using message dialog. isExist() function in the file class helps to check whether a file/directory exists.

Download Project  

Main Window screenshot for Java File Explorer
Loaded C Drive
D drive listed using Java File Explorer
Using Go to to load D drive
Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.


  1. The Spring Framework is a lightweight framework for developing Java enterprise applications. It provides high performing, easily testable and reusable code. Spring handles the infrastructure as the underlying framework so that you can focus on your application.Spring is modular in design, thereby making creation, handling and linking of individual components so much easier. Spring implements Model View Container(MVC) design pattern.spring mvc validation example

  2. Hi Good day….

    Excellent post and very useful for everyone…keep updating
    php training in coimbatore

    seo training in coimbatore

    web design training coimbatore

    digital marketing training in coimbatore