Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

In this page you can find the example usage for java.io File listFiles.

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static boolean deleteFolder(File Folder, boolean AndFolder) {
    boolean failed = false;
    if (Folder.exists() && Folder.isDirectory()) {
        File[] files = Folder.listFiles();
        if (files != null) {
            for (File i : files) {
                if (i.isDirectory()) {
                    /** Recursive delete */
                    failed = failed || !deleteFolder(i, AndFolder);
                } else {
                    failed = failed || !i.delete();
                }/*from  w  w  w  .j av a2s.  c o m*/
            }
        }
        if (AndFolder) {
            failed = failed || !Folder.delete();
        }
    }
    return !failed;
}

From source file:Main.java

public static void copyAsset(Context context, AssetManager am, boolean force) {
    File newDir = getSDDir(context);
    File sd = Environment.getExternalStorageDirectory();
    if (sd != null && sd.exists() && sd.isDirectory() && newDir.exists() && newDir.isDirectory()) {
        File hpDir = new File(sd, ".hp48");
        if (hpDir.exists()) {
            File allFiles[] = hpDir.listFiles();
            if (allFiles != null && allFiles.length > 0) {
                Log.i("x48",
                        "Moving x48 files from the old dir " + sd.getAbsolutePath() + " to the proper one :");
                for (File file : allFiles) {
                    File newFile = new File(newDir, file.getName());
                    Log.i("x48", "Moving " + file.getAbsolutePath() + " to " + newFile);
                    file.renameTo(newFile);
                }/*from ww w  .j a  v  a 2 s . co m*/
            }
            Log.i("x48", "Deleting old directory");
            hpDir.delete();
        }
    }
    copyAsset(am, newDir, force);
}

From source file:Main.java

private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException {
    if (srcFile.isDirectory()) {
        File subfile[] = srcFile.listFiles();
        for (int i = 0; i < subfile.length; i++) {
            compressDir(subfile[i], out, destPath);
        }/*from www . j  ava  2 s  .co m*/
    } else {
        InputStream in = new FileInputStream(srcFile);
        String name = srcFile.getAbsolutePath().replace(destPath, "");
        if (name.startsWith("\\"))
            name = name.substring(1);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(srcFile.length());
        entry.setTime(srcFile.lastModified());
        out.putNextEntry(entry);
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);

        out.closeEntry();
        in.close();
    }
}

From source file:Main.java

public static LinkedList<File> listLinkedFiles(String strPath) {
    LinkedList<File> list = new LinkedList<File>();
    File dir = new File(strPath);
    if (!dir.isDirectory()) {
        return null;
    }/*from  w  w w .  jav a2  s . com*/
    File file[] = dir.listFiles();
    for (int i = 0; i < file.length; i++) {
        if (file[i].isDirectory())
            list.add(file[i]);
        else
            System.out.println(file[i].getAbsolutePath());
    }
    File tmp;
    while (!list.isEmpty()) {
        tmp = (File) list.removeFirst();
        if (tmp.isDirectory()) {
            file = tmp.listFiles();
            if (file == null)
                continue;
            for (int i = 0; i < file.length; i++) {
                if (file[i].isDirectory())
                    list.add(file[i]);
                else
                    System.out.println(file[i].getAbsolutePath());
            }
        } else {
            System.out.println(tmp.getAbsolutePath());
        }
    }
    return list;
}

From source file:co.marcin.novaguilds.util.IOUtils.java

public static List<String> getFilesWithoutExtension(File directory) {
    List<String> list = new ArrayList<>();
    File[] filesList = directory.listFiles();

    if (filesList != null) {
        for (File file : filesList) {
            if (file.isFile()) {
                String name = file.getName();
                if (org.apache.commons.lang.StringUtils.contains(name, '.')) {
                    name = org.apache.commons.lang.StringUtils.split(name, '.')[0];
                    list.add(name);/*w w  w. j a  va  2 s  .  co  m*/
                }
            }
        }
    }

    return list;
}

From source file:Main.java

public static void copyDir(String src, String dst) {
    try {//  w  w w . j a  v  a  2s  .  c om
        File fileSrc = new File(src);
        if (!fileSrc.exists()) {
            return;
        }
        File[] filelist = fileSrc.listFiles();
        File fileDst = new File(dst);
        if (!fileDst.exists()) {
            fileDst.mkdirs();
        }
        for (File f : filelist) {
            if (f.isDirectory()) {
                copyDir(f.getPath() + "/", dst + f.getName() + "/");
            } else {
                copyFile(f.getPath(), dst + f.getName());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.uci.ics.asterix.test.runtime.ExecutionTest.java

@AfterClass
public static void tearDown() throws Exception {
    AsterixHyracksIntegrationUtil.deinit();
    File outdir = new File(PATH_ACTUAL);
    File[] files = outdir.listFiles();
    if (files == null || files.length == 0) {
        outdir.delete();//from  w ww .  jav a  2  s. c o  m
    }
    // clean up the files written by the ASTERIX storage manager
    for (String d : ASTERIX_DATA_DIRS) {
        TestsUtils.deleteRec(new File(d));
    }
    HDFSCluster.getInstance().cleanup();
}

From source file:Main.java

private static void mergeExeBatchFiles() {
    File file = new File(batchDir);
    System.out.println("debug:current path = " + file.getAbsolutePath());
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return;//from www  . j  av a 2s .  co m
    }
    Arrays.sort(files, new Comparator<File>() {
        public int compare(File file1, File file2) {
            if (file1.lastModified() > file2.lastModified()) {
                return 1;
            } else if (file1.lastModified() < file2.lastModified()) {
                return -1;
            } else {
                return 0;
            }
        }
    });
    StringBuffer command = new StringBuffer();
    for (File f : files) {
        command.append("call ").append(f.getAbsolutePath()).append("\n");
    }
    try {
        String filePath = batchDir + "build.bat";
        writeFile(filePath, command.toString());
        Runtime.getRuntime().exec("cmd /c start " + filePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Util.java

public static long getFileSizeInBytes(String fileName) {
    long ret = 0;
    File f = new File(fileName);
    if (f.isFile()) {
        return f.length();
    } else if (f.isDirectory()) {
        File[] contents = f.listFiles();
        for (int i = 0; i < contents.length; i++) {
            if (contents[i].isFile()) {
                ret += contents[i].length();
            } else if (contents[i].isDirectory())
                ret += getFileSizeInBytes(contents[i].getPath());
        }//from  ww w .j a  va  2s  . c om
    }
    return ret;
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

private static void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            addFolderToZip(file, zip, baseName);
        } else {/*from ww w  .jav  a  2  s.  c  o  m*/
            String name = file.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name);
            zip.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(file), zip);
            zip.closeEntry();
        }
    }
}