Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java File Explorer program with Source Code


Let’s develop a basic file explorer using java. Java’s FILE Class from java.io 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.

Desktop.getDesktop().open(FileName); 

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.
 container.revalidate();  
container.repaint();

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

Download Project 

Suggested Read : Java Barcode Generator - Java Library Management Program


This post first appeared on The Genuine Coder, please read the originial post: here

Share the post

Java File Explorer program with Source Code

×

Subscribe to The Genuine Coder

Get updates delivered right to your inbox!

Thank you for your subscription

×