Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

public class Main {
    public static long getFileSize(File dirOrFile) {

        if (dirOrFile == null || !dirOrFile.exists()) {
            return 0;
        }

        if (dirOrFile.isFile()) {
            return dirOrFile.length();
        }

        if (dirOrFile.isDirectory()) {
            File[] subDirOrFiles = dirOrFile.listFiles();
            if (subDirOrFiles == null || subDirOrFiles.length <= 0) {
                return 0;
            }
            long size = 0;
            for (File subDirOrFile : subDirOrFiles) {
                size += getFileSize(subDirOrFile);
            }
            return size;
        }

        return 0;
    }
}