Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.util.*;

public class Main {
    /**
     0: number of files
     1: total length
     2: number of directories
     */
    public static void getDirSize(final File f, final long[] l) {
        if (f.isFile()) {
            l[0]++;
            l[1] += f.length();
        } else {
            Stack<File> stk = new Stack<>();
            stk.add(f);
            File fi = null;
            File[] fs;
            while (stk.size() > 0) {
                fi = stk.pop();
                fs = fi.listFiles();
                for (File f2 : fs) {
                    if (f2.isDirectory()) {
                        stk.push(f2);
                        l[2]++;
                    } else {
                        l[0]++;
                        l[1] += f2.length();
                    }
                }
            }
        }
    }
}