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:Main.java

public static boolean deleteDir(@NonNull File dir) {
    if (dir != null && 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;
            }/*from w  w  w .j a va2  s  .  com*/
        }
    }
    if (dir == null) {
        return false;
    }
    return dir.delete();
}

From source file:Main.java

private static void deleteDirectoryContent(String directoryPath) {
    File dir = new File(directoryPath);
    if (dir.exists() && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            File file = new File(dir, children[i]);
            if (file.isDirectory()) {
                deleteDirectoryContent(file.getAbsolutePath());
            } else {
                file.delete();/*from  w  w w  .  j  a va 2s  .  com*/
            }
        }
        dir.delete();
    }
}

From source file:com.intropro.prairie.utils.FileUtils.java

public static List<String> readLineInDirectory(File file) throws IOException {
    List<String> result = new LinkedList<>();
    for (String child : file.list()) {
        File childFile = new File(file, child);
        if (childFile.isDirectory()) {
            result.addAll(readLineInDirectory(childFile));
        } else {/*from   w  w w .  jav  a2s .  c o m*/
            result.addAll(IOUtils.readLines(new FileInputStream(childFile)));
        }
    }
    return result;
}

From source file:Main.java

public static void deleteSharedPreferences(Context context) {
    try {/*w w  w  .  ja v a 2 s.  com*/
        Context appContext = context.getApplicationContext();
        ApplicationInfo info = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(),
                0);
        String dirPath = info.dataDir + File.separator + "shared_prefs" + File.separator;
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            String[] list = dir.list();
            int size = list.length;
            for (int i = 0; i < size; i++) {
                new File(dirPath + list[i]).delete();
            }
        } else {
            //Log.d("AAA", "NO FILE or NOT DIR");
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static float getSize(String path, Float size) {
    File file = new File(path);
    if (file.exists()) {
        if (file.isDirectory()) {
            String[] children = file.list();
            for (int fileIndex = 0; fileIndex < children.length; ++fileIndex) {
                float tmpSize = getSize(file.getPath() + File.separator + children[fileIndex], size) / 1000;
                size += tmpSize;//from   w ww. j  a v  a2 s  .com
            }
        } else if (file.isFile()) {
            size += file.length();
        }
    }
    return size;
}

From source file:com.gc.iotools.fmt.base.TestUtils.java

public static String[] listFilesExcludingExtension(final String[] forbidden) throws IOException {
    final URL fileURL = TestUtils.class.getResource("/testFiles");
    String filePath = URLDecoder.decode(fileURL.getPath(), "UTF-8");
    final File dir = new File(filePath);
    final String[] files = dir.list();
    final Collection<String> goodFiles = new Vector<String>();
    if (!filePath.endsWith(File.separator)) {
        filePath = filePath + File.separator;
    }// ww w  .ja v a  2s  .c o m
    for (final String file : files) {
        boolean insert = true;
        for (final String extForbidden : forbidden) {
            insert &= !(file.endsWith(extForbidden));
        }
        if (insert) {
            goodFiles.add(filePath + file);
        }
    }
    return goodFiles.toArray(new String[goodFiles.size()]);
}

From source file:com.gc.iotools.fmt.base.TestUtils.java

/**
 * @deprecated/*w ww. j av  a  2  s . c o m*/
 * @see FileUtils#iterate();
 * @param allowed
 * @return
 * @throws IOException
 */
@Deprecated
public static String[] listFilesIncludingExtension(final String[] allowed) throws IOException {
    final URL fileURL = TestUtils.class.getResource("/testFiles");
    String filePath = URLDecoder.decode(fileURL.getPath(), "UTF-8");
    final File dir = new File(filePath);
    final String[] files = dir.list();
    final Collection<String> goodFiles = new Vector<String>();
    if (!filePath.endsWith(File.separator)) {
        filePath = filePath + File.separator;
    }
    for (final String file : files) {
        for (final String element : allowed) {
            if (file.endsWith(element)) {
                goodFiles.add(filePath + file);
            }
        }
    }
    return goodFiles.toArray(new String[goodFiles.size()]);
}

From source file:Main.java

public static void copyDirFile(String oldPath, String newPath) {
    try {/*w  w  w  . j a  va  2s.c o m*/
        new File(newPath).mkdirs();
        File a = new File(oldPath);
        System.out.println("oldPath:" + oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyDirFile(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean deleteFile(String path) {
    File file = new File(path);
    if (!file.exists())
        return true;

    if (file.isDirectory()) {
        String[] subPaths = file.list();
        for (int i = 0; i < subPaths.length; i++) {
            if (!deleteFile(path)) {
                return false;
            }/*w  w  w .  j  a v a  2s .c  o  m*/
        }
    }

    return file.delete();
}

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Not a directory:  " + dir);
    String[] entries = d.list();
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();//from   w  w  w.ja v  a2  s .com
    }
    out.close();
}