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 android.util.*;
import java.io.*;

import java.util.*;

public class Main {
    public static List<File> listFilesByName(File f) {
        Log.d("listFileByName f", "" + f);
        final List<File> fList = new LinkedList<>();
        final Stack<File> stk = new Stack<>();
        if (f.isDirectory()) {
            stk.push(f);
        } else {
            fList.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);
                } else {
                    fList.add(f2);
                }
            }
        }
        return fList;
    }

    public static StringBuilder listFilesByName(File f, String lineSep) {
        Log.d("listFileByName f", "" + f);
        StringBuilder sb = new StringBuilder();
        if (f != null) {
            final Stack<File> stk = new Stack<>();
            if (f.isDirectory()) {
                stk.push(f);
            } else {
                sb.append(f.getAbsolutePath()).append(": ").append(f.length()).append(" bytes.").append(lineSep);
            }
            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);
                    } else {
                        sb.append(f2.getAbsolutePath()).append(": ").append(f2.length()).append(" bytes.")
                                .append(lineSep);
                    }
                }
            }

        }
        return sb;
    }
}