Display Recursively Files and Folder List in Java

 Posted On  | Yashwant Chavan 

Recently I am working on the some java file operation task,Requirement is so simple , program should reads all the files and folders. There are thousands of files generated on hard drive on daily basis so my program need to delete all those files whose file-size is more than 10MB.This clean up executed on daily basis and delete the respected files.

Display List OF Files (ListFilesAndFolder.java)

So as per my requirement I have to read all the files and folder. Below sample code read all files and folders using Recursion and find out the desired file. listFiles() method of file object return all child's are available in that directory.

Java list all files in a folder

package com.technicalkeeda.app;

import java.io.File;
import java.text.DecimalFormat;

public class ListFilesAndFolder {
    public static void main(String[] args) {
        list("C:\\hello");
    }

    public static void list(String path) {

        File root = new File(path);
        File[] list = root.listFiles();

        for (File f: list) {
            if (f.isDirectory()) {
                System.out.println("Folder : [ " + f.getAbsoluteFile() + "]");
                list(f.getAbsolutePath());
            } else {
                String filesize = displayFilesize(f.length());
                System.out.println("Folder : [" + f.getParent() +
                    "] File Name : [" + f.getAbsoluteFile() +
                    "] File-Size [" + (filesize) + "]");
                if (f.length() / (1024 * 1024) > 10) { // more than 10 mb
                    f.delete();
                }

            }
        }
    }

    public static String displayFilesize(long fileSize) {
        if (fileSize <= 0) {
            return "0";
        } else {
            final String[] fileUnit = new String[] {
                "B",
                "KB",
                "MB",
                "GB",
                "TB"
            };
            int group = (int)(Math.log10(fileSize) / Math.log10(1024));
            return new DecimalFormat("#,##0.#").format(fileSize /
                    Math.pow(1024, group)) +
                " " + fileUnit[group];
        }
    }
}

Display File Size in the Byte, KB, MB, GB, and TB

Below code snippet format and display the file size

public static String displayFilesize(long fileSize) {
        if (fileSize <= 0) {
            return "0";
        } else {
            final String[] fileUnit = new String[] {
                "B",
                "KB",
                "MB",
                "GB",
                "TB"
            };
            int group = (int)(Math.log10(fileSize) / Math.log10(1024));
            return new DecimalFormat("#,##0.#").format(fileSize /
                    Math.pow(1024, group)) +
                " " + fileUnit[group];
        }
    }

Output

 Folder : [ C:\hello\one]
 Folder : [C:\hello\one] File Name : [C:\hello\one\1.jpg] File-Size [166 KB]
 Folder : [C:\hello\one] File Name : [C:\hello\one\2.jpg] File-Size [100.5 KB]
 Folder : [ C:\hello\two]
 Folder : [C:\hello\two] File Name : [C:\hello\two\22.jpg] File-Size [166 KB]
 Folder : [C:\hello\two] File Name : [C:\hello\two\33.jpg] File-Size [100.5 KB]


Promote your Blog, Website, or services on technicalkeeda.com, My paypal id: yashwantchavan@gmail.com

Payment will be accepted through paypal only.


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0293 seconds.