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.
Loaded C Drive |
Using Go to to load D drive |
Suggested Read : Java Barcode Generator – Java Library Management Program
Thanks for sharing this information in here. As a fresher any one can also go for the below training programs.
Java Training in Chennai|Java Courses in Chennai
Wonderful post.This is really one of the most beneficial blogs I’ve ever browsed on this subject. I am very glad to read such a great blog and thank you for sharing this good information with us.
Regards,
JAVA Training in Chennai | Java Courses in Chennai | J2EE Training in Chennai
I have read your blog its very attractive and impressive. I like it your blog.
Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai
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
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
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] There you can find 20370 more Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More Information here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] There you will find 39214 additional Information on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More on on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] There you will find 32247 more Information to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]
… [Trackback]
[…] Find More to that Topic: genuinecoder.com/directory-folder-indexing-program-using-java-source-code-html/ […]