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

/**
 * Zip up a directory path// ww  w  .j  av  a2 s. c om
 * @param directory
 * @param zos
 * @param path
 * @throws IOException
 */
public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException {
    File zipDir = new File(directory);
    // get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            String filePath = f.getPath();
            zipDir(filePath, zos, path + f.getName() + "/");
            continue;
        }
        FileInputStream fis = new FileInputStream(f);
        try {
            ZipEntry anEntry = new ZipEntry(path + f.getName());
            zos.putNextEntry(anEntry);
            bytesIn = fis.read(readBuffer);
            while (bytesIn != -1) {
                zos.write(readBuffer, 0, bytesIn);
                bytesIn = fis.read(readBuffer);
            }
        } finally {
            fis.close();
        }
    }
}

From source file:Main.java

public static void deleteFile(String fileName) {
    File f = new File(fileName);
    if (f.exists()) {
        if (f.isFile()) {
            f.delete();//from   w  w  w.  ja  v a 2 s  .  c  o m
        } else if (f.isDirectory()) {
            String[] filelist = f.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(fileName + filelist[i]);
                deleteFile(fileName + filelist[i]);
            }
        }
    }
}

From source file:com.streamsets.datacollector.cluster.TarFileCreator.java

/**
 * Copied from https://raw.githubusercontent.com/kamranzafar/jtar/master/src/test/java/org/kamranzafar/jtar/JTarTest.java
 *///from  w w  w .j  av  a  2 s .  c  o  m
private static void tarFolder(String parent, String path, TarOutputStream out) throws IOException {
    BufferedInputStream src = null;
    File f = new File(path);
    String files[] = f.list();
    // is file
    if (files == null) {
        files = new String[1];
        files[0] = f.getName();
    }
    parent = ((parent == null) ? (f.isFile()) ? "" : f.getName() + "/" : parent + f.getName() + "/");
    for (int i = 0; i < files.length; i++) {
        File fe = f;
        if (f.isDirectory()) {
            fe = new File(f, files[i]);
        }
        if (fe.isDirectory()) {
            String[] fl = fe.list();
            if (fl != null && fl.length != 0) {
                tarFolder(parent, fe.getPath(), out);
            } else {
                TarEntry entry = new TarEntry(fe, parent + files[i] + "/");
                out.putNextEntry(entry);
            }
            continue;
        }
        FileInputStream fi = new FileInputStream(fe);
        src = new BufferedInputStream(fi);
        TarEntry entry = new TarEntry(fe, parent + files[i]);
        out.putNextEntry(entry);
        IOUtils.copy(src, out);
        src.close();
        out.flush();
    }
}

From source file:Main.java

/**
 * zips a directory/*w  w w  .  j av  a  2 s .c  o m*/
 * @param dir2zip
 * @param zipOut
 * @param zipFileName
 * @throws IOException
 */
private static void zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName) throws IOException {
    File zipDir = new File(dir2zip);
    // get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            // if the File object is a directory, call this
            // function again to add its content recursively
            String filePath = f.getPath();
            zipDir(filePath, zipOut, zipFileName);
            // loop again
            continue;
        }

        if (f.getName().equals(zipFileName)) {
            continue;
        }
        // if we reached here, the File object f was not a directory
        // create a FileInputStream on top of f
        final InputStream fis = new BufferedInputStream(new FileInputStream(f));
        try {
            ZipEntry anEntry = new ZipEntry(f.getPath().substring(dir2zip.length() + 1));
            // place the zip entry in the ZipOutputStream object
            zipOut.putNextEntry(anEntry);
            // now write the content of the file to the ZipOutputStream
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zipOut.write(readBuffer, 0, bytesIn);
            }
        } finally {
            // close the Stream
            fis.close();
        }
    }
}

From source file:model.manager.FileManager.java

/**
 * Method to find all xml files in a given directory.
 * //from  ww w. j  ava2 s.c o  m
 * @param rootPath
 * @return List<String>
 */
public static String[] getDataExportObjects(String rootPath) {

    File dir = new File(rootPath);

    String[] children = dir.list();

    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir1, String name) {
            return name.endsWith(".xml");
        }
    };
    children = dir.list(filter);
    return children;
}

From source file:com.google.gdt.eclipse.designer.gxt.actions.ConfigureExtGwtOperation.java

static String getJarPath(String libraryLocation) {
    File libraryFolder = new File(libraryLocation);
    for (String jarName : libraryFolder.list()) {
        String jarNameLower = jarName.toLowerCase();
        if (jarNameLower.startsWith("gxt") && jarNameLower.endsWith(".jar")) {
            if (jarNameLower.endsWith("-gwt22.jar")) {
                return libraryLocation + "/" + jarName;
            }/*  w  ww  .  j ava 2  s.  c  o  m*/
        }
    }
    return libraryLocation + "/gxt.jar";
}

From source file:Main.java

public static void delete(File file) throws IOException {

    if (file.isDirectory()) {

        // directory is empty, then delete it
        if (file.list().length == 0) {

            file.delete();//  ww w.  j  a  va  2  s .c  om
            System.out.println("Directory is deleted : " + file.getAbsolutePath());

        } else {

            // list all the directory contents
            String files[] = file.list();

            for (String temp : files) {
                // construct the file structure
                File fileDelete = new File(file, temp);

                // recursive delete
                delete(fileDelete);
            }

            // check the directory again, if empty then delete it
            if (file.list().length == 0) {
                file.delete();
                System.out.println("Directory is deleted : " + file.getAbsolutePath());
            }
        }

    } else {
        // if file, then delete it
        file.delete();
        System.out.println("File is deleted : " + file.getAbsolutePath());
    }
}

From source file:com.glaf.core.context.ApplicationContext.java

public static String getAppClasspath() {
    if (classPath == null) {
        StringBuffer buffer = new StringBuffer(5000);
        String root = getAppPath();
        String path = root + sp + "WEB-INF" + sp + "lib";
        java.io.File file = new java.io.File(path);
        if (file.isDirectory()) {
            String[] filelist = file.list();
            if (filelist != null) {
                for (int i = 0, len = filelist.length; i < len; i++) {
                    if (filelist[i].endsWith(".jar")) {
                        String filename = path + sp + filelist[i];
                        buffer.append(filename).append(';');
                    }//w  w w. jav a2  s .c  om
                }
            }
        }
        classPath = buffer.toString();
    }
    return classPath;
}

From source file:com.pnf.jebauto.AutoUtil.java

private static void retrieveFilesRecurse(File f, List<File> results) {
    if (f.isFile()) {
        results.add(f);/*from   ww w  .j av a 2 s . c o m*/
    } else {
        for (String name : f.list()) {
            File f1 = new File(f, name);
            retrieveFilesRecurse(f1, results);
        }
    }
}

From source file:Main.java

public static void deleteRecursively(File target) throws IOException {
    if (!target.exists())
        return;/* w w  w .  j a v a  2s  .c o m*/

    if (target.isDirectory()) {
        String[] children = target.list();
        for (int i = 0; i != children.length; ++i)
            deleteRecursively(new File(target, children[i]));
    }

    //platformLog("delete", target.getPath());

    if (!target.delete())
        throw new IOException("Can not delete " + target.getAbsolutePath());
}