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:de.ingrid.iplug.csw.dsc.tools.FileUtils.java

/**
 * This function will copy files or directories from one location to another.
 * note that the source and the destination must be mutually exclusive. This 
 * function can not be used to copy a directory to a sub directory of itself.
 * The function will also have problems if the destination files already exist.
 * @param src -- A File object that represents the source for the copy
 * @param dest -- A File object that represnts the destination for the copy.
 * @throws IOException if unable to copy.
 * /*from  w  w  w .j a  va  2  s  .  co m*/
 * Source: http://www.dreamincode.net/code/snippet1443.htm
 */
public static void copyRecursive(File src, File dest) throws IOException {
    //Check to ensure that the source is valid...
    if (!src.exists()) {
        throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) { //check to ensure we have rights to the source...
        throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
    }
    //is this a directory copy?
    if (src.isDirectory()) {
        if (!dest.exists()) { //does the destination already exist?
            //if not we need to make it exist if possible (note this is mkdirs not mkdir)
            if (!dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
        }
        //get a listing of files...
        String list[] = src.list();
        //copy all the files in the list.
        for (int i = 0; i < list.length; i++) {
            File dest1 = new File(dest, list[i]);
            File src1 = new File(src, list[i]);
            copyRecursive(src1, dest1);
        }
    } else {
        //This was not a directory, so lets just copy the file
        FileInputStream fin = null;
        FileOutputStream fout = null;
        byte[] buffer = new byte[4096]; //Buffer 4K at a time (you can change this).
        int bytesRead;
        try {
            //open the files for input and output
            fin = new FileInputStream(src);
            fout = new FileOutputStream(dest);
            //while bytesRead indicates a successful read, lets write...
            while ((bytesRead = fin.read(buffer)) >= 0) {
                fout.write(buffer, 0, bytesRead);
            }
            fin.close();
            fout.close();
            fin = null;
            fout = null;
        } catch (IOException e) { //Error copying file... 
            IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath()
                    + "to" + dest.getAbsolutePath() + ".");
            wrapper.initCause(e);
            wrapper.setStackTrace(e.getStackTrace());
            throw wrapper;
        } finally { //Ensure that the files are closed (if they were open).
            if (fin != null) {
                fin.close();
            }
            if (fout != null) {
                fin.close();
            }
        }
    }
}

From source file:de.u808.simpleinquest.util.DirectoryTraverser.java

private void processRootDirectory(File dir) {
    String[] files = dir.list();
    if (files != null) {
        for (String fileName : files) {
            File file = new File(dir, fileName);
            this.processFile(file);
        }/*from   w ww .  j  a  v  a2  s .  c om*/
    } else {
        String rootDDir = (dir == null ? "Null" : dir.getAbsolutePath());
        log.info("No files fount to process in directory: " + rootDDir);
    }
}

From source file:com.pari.nm.utils.backup.BackupRestore.java

public static boolean deleteDirContents(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();

        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDirContents(new File(dir, children[i]));

            if (!success) {
                return false;
            }//from w  w w .  j av a  2s  .co m
        }
    }

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

From source file:camelinaction.SpringFileRollbackTest.java

@Test
public void testOk() throws Exception {
    template.sendBodyAndHeader("direct:confirm", "bumper", "to", "someone@somewhere.org");

    File file = new File("target/mail/backup/");
    String[] files = file.list();
    assertEquals("There should be one file", 1, files.length);
}

From source file:FileTable2.java

public void setFileStats(File dir) {
    String files[] = dir.list();
    data = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
        File tmp = new File(files[i]);
        data[i][0] = new Boolean(tmp.isDirectory());
        data[i][1] = tmp.getName();/*w ww  .  j a va 2s .  com*/
        data[i][2] = new Boolean(tmp.canRead());
        data[i][3] = new Boolean(tmp.canWrite());
        data[i][4] = new Long(tmp.length());
        data[i][5] = new Date(tmp.lastModified());
    }

    // Just in case anyone's listening...
    fireTableDataChanged();
}

From source file:cognition.pipeline.commandline.CommandRunFromFileSystem.java

@Override
public void process(CommandLine cmd) {
    folderAbsolutePath = cmd.getOptionValue("folder");
    File folder = new File(folderAbsolutePath);
    String[] files = folder.list();
    List<String> fileList = new ArrayList<>();
    fileList.addAll(Arrays.asList(files));

    fileList.parallelStream().forEach(file -> {
        String absoluteFilePath = folderAbsolutePath + file;
        logger.info("Processing " + absoluteFilePath);
        dncPipelineService.processFile(absoluteFilePath);
    });//from ww  w . j  a  v a 2 s.  co m

    logger.info("Finished all files in directory " + folderAbsolutePath);
}

From source file:camelinaction.SpringFileRollbackTest.java

@Test
public void testRollback() throws Exception {
    try {/*from ww  w  . j  av  a2  s.c  o  m*/
        template.sendBodyAndHeader("direct:confirm", "bumper", "to", "FATAL");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
        assertEquals("Simulated fatal error", e.getCause().getMessage());
    }

    // give time for onCompletion to execute
    // as its being executed asynchronously in another thread
    Thread.sleep(1000);

    File file = new File("target/mail/backup/");
    String[] files = file.list();
    assertEquals("There should be no files", 0, files.length);
}

From source file:CreatingTreeModel.java

public Object getChild(Object parent, int index) {
    File directory = (File) parent;
    String[] directoryMembers = directory.list();
    return (new File(directory, directoryMembers[index]));
}

From source file:org.broadleafcommerce.admin.util.controllers.RemoveFileController.java

private boolean deleteDir(File dir) {
    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;
            }/*from   w  ww  .j a v  a  2 s .co m*/
        }
    }

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

From source file:FileTreeFrame.java

public Object getChild(Object parent, int index) {
    File directory = (File) parent;
    String[] children = directory.list();
    return new TreeFile(directory, children[index]);
}