Example usage for java.io File list

List of usage examples for java.io File list

Introduction

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

Prototype

public String[] list() 

Source Link

Document

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

Usage

From source file:com.aspose.email.examples.outlook.pst.SplitAndMergePSTFile.java

public static void deleteAllFilesInDirectory(File dir) {
    for (String s : dir.list()) {
        File currentFile = new File(dir.getPath(), s);
        currentFile.delete();//from   w  w  w .j  a va  2s.  com
    }
}

From source file:Main.java

public static long getFolderSize(File folder) throws IllegalArgumentException {
    // Validate/*from  w  ww  .  j  ava2s.  c  om*/
    if (folder == null || !folder.isDirectory())
        throw new IllegalArgumentException("Invalid   folder ");
    String list[] = folder.list();
    if (list == null || list.length < 1)
        return 0;

    // Get size
    File object = null;
    long folderSize = 0;
    for (int i = 0; i < list.length; i++) {
        object = new File(folder, list[i]);
        if (object.isDirectory())
            folderSize += getFolderSize(object);
        else if (object.isFile())
            folderSize += object.length();
    }
    return folderSize;
}

From source file:com.ikon.util.FileUtils.java

/**
 * Delete directory if empty//from   www  . j a v  a2  s.  c  om
 */
public static void deleteEmpty(File file) {
    if (file.isDirectory()) {
        if (file.list().length == 0) {
            file.delete();
        }
    }
}

From source file:Main.java

public static boolean deleteDir(File dir) {
    try {//from   w ww. j a  v  a2 s. com
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();

    } catch (Exception e) {
        Log.e("Haggle", "Failed to delete " + dir + " : " + e);
        return false;
    }
}

From source file:com.jcalvopinam.core.Unzipping.java

private static void joinAndUnzipFile(File inputFile, CustomFile customFile) throws IOException {

    ZipInputStream is = null;/*  w  w w.jav  a  2 s.  co  m*/
    File path = inputFile.getParentFile();

    List<InputStream> fileInputStream = new ArrayList<>();
    for (String fileName : path.list()) {
        if (fileName.startsWith(customFile.getFileName()) && fileName.endsWith(Extensions.ZIP.getExtension())) {
            fileInputStream.add(new FileInputStream(String.format("%s%s", customFile.getPath(), fileName)));
            System.out.println(String.format("File found: %s", fileName));
        }
    }

    if (fileInputStream.size() > 0) {
        String fileNameOutput = String.format("%s%s", customFile.getPath(), customFile.getFileName());
        OutputStream os = new BufferedOutputStream(new FileOutputStream(fileNameOutput));
        try {
            System.out.println("Please wait while the files are joined: ");

            ZipEntry ze;
            for (InputStream inputStream : fileInputStream) {
                is = new ZipInputStream(inputStream);
                ze = is.getNextEntry();
                customFile.setFileName(String.format("%s_%s", REBUILT, ze.getName()));

                byte[] buffer = new byte[CustomFile.BYTE_SIZE];

                for (int readBytes; (readBytes = is.read(buffer, 0, CustomFile.BYTE_SIZE)) > -1;) {
                    os.write(buffer, 0, readBytes);
                    System.out.print(".");
                }
            }
        } finally {
            os.flush();
            os.close();
            is.close();
            renameFinalFile(customFile, fileNameOutput);
        }
    } else {
        throw new FileNotFoundException("Error: The file not exist!");
    }
    System.out.println("\nEnded process!");
}

From source file:org.meshpoint.anode.util.ModuleUtils.java

private static boolean deleteDir(File location) {
    for (String child : location.list()) {
        boolean result = deleteFile(new File(location, child));
        if (!result) {
            return false;
        }/*from  ww w  . j a v  a  2 s.  com*/
    }
    return location.delete();
}

From source file:com.photon.phresco.util.FileUtil.java

private static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory() && dir.exists()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }//from w  w w  .j a  v  a  2  s.co m
        }
    }

    return dir.delete();
}

From source file:com.microsoft.azurebatch.jenkins.utils.ZipHelper.java

private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
    File folder = new File(srcFolder);

    // Check the empty folder
    String[] files = folder.list();
    if (files == null || files.length == 0) {
        addFileToZip(path, srcFolder, zip, true);
    } else {//from   w  w  w .  ja  va 2  s . c o  m
        // List the files in the folder
        for (String fileName : files) {
            if (path.equals("")) {
                addFileToZip(folder.getName(), srcFolder + File.separator + fileName, zip, false);
            } else {
                addFileToZip(path + File.separator + folder.getName(), srcFolder + File.separator + fileName,
                        zip, false);
            }
        }
    }
}

From source file:edu.psu.citeseerx.disambiguation.CsxDisambiguation.java

public static void disambiguateDirectory(ListableBeanFactory factory, String indir, String outdir)
        throws Exception {

    File dir = new File(indir);
    String[] files = dir.list();
    Arrays.sort(files);//w  w  w . ja v  a2 s  . co m
    for (String file : files) {
        String infile = indir + "/" + file;
        String outfile = outdir + "/" + file.replace(".txt", ".out");
        System.out.println("> " + file);
        disambiguate(factory, infile, outfile);
    }
}

From source file:Main.java

/**
 * Remove a directory and all of its contents. The results of executing
 * File.delete() on a File object that represents a directory seems to be
 * platform dependent. This method removes the directory and all of its
 * contents.//from  w  ww .j  a v a 2  s .c  om
 * 
 * @return true if the complete directory was removed, false if it could not
 *         be. If false is returned then some of the files in the directory
 *         may have been removed.
 */
public static boolean removeDirectory(File directory) {
    if (directory == null)
        return false;
    if (!directory.exists())
        return true;
    if (!directory.isDirectory())
        return false;
    String[] list = directory.list();
    // Some JVMs return null for File.list() when the
    // directory is empty.
    if (list != null) {
        for (int i = 0; i < list.length; i++) {
            File entry = new File(directory, list[i]);
            if (entry.isDirectory()) {
                if (!removeDirectory(entry))
                    return false;
            } else {
                if (!entry.delete())
                    return false;
            }
        }
    }
    return directory.delete();
}