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.arvato.thoroughly.util.CommonUtils.java

/**
 * Read all the files in a folder//from w w w . j av  a 2 s . c  o  m
 *
 * @param filepath
 * @param character
 *           Fuzzy query based on the file name
 * @return key=filename<br/>
 *         value=filecontent
 */
public static Map<String, String> readFileContentOfTheFloder(String filepath, String character) {

    File file = new File(filepath);
    Map<String, String> result = new HashMap<String, String>();
    if (file.isDirectory()) {
        String[] filelist = file.list();
        for (int i = 0; i < filelist.length; i++) {
            File readfile = new File(filepath + File.separator + filelist[i]);
            if (!readfile.isDirectory()) {

                String tempFilePath = readfile.getAbsolutePath();
                String tempfileName = readfile.getName();

                if (StringUtils.isNotEmpty(character)) {
                    if (tempfileName.contains(character)) {
                        result.put(tempfileName, readFile(tempFilePath));
                    }
                } else {
                    result.put(tempfileName, readFile(tempFilePath));
                }
            }
        }
    }
    return result;
}

From source file:fr.shywim.antoinedaniel.utils.Utils.java

/**
 * Delete a directory.//from w w w .j av a 2  s.c o  m
 *
 * @param dir the directory to delete
 * @return whether the directory has been successfully deleted or not
 */
@SuppressWarnings("ObjectAllocationInLoop")
public static boolean deleteDir(File dir) {
    String[] children = dir.list();
    if (children != null) {
        for (String aChildren : children) {
            File file = new File(dir, aChildren);
            //noinspection ResultOfMethodCallIgnored
            file.delete();
        }
    }
    return dir.delete();
}

From source file:localization.split.java

public static boolean splitFile(String filepath, String passoloPath, boolean myesri) {
    File log = null;/*w ww  .ja  v a  2s .co m*/
    try {
        String path = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length());
        String folder = filepath.substring(0, filepath.lastIndexOf("\\"));
        String[] files = null;
        Vector<String> zFile;
        if (filepath.endsWith(".zip")) {
            zFile = readzipfile(filepath);
            for (String s : zFile) {
                if (s.endsWith(".lpu")) {
                    File unzipFolder = new File(s.substring(0, s.lastIndexOf("\\")));
                    splitFile(s, passoloPath, myesri);
                }
            }
        } else if (!filepath.endsWith(".lpu")) {
            return false;
        } else {
            if (!checkLPU(filepath, passoloPath)) {
                return false;
            }
            File ECI = new File(folder + "\\ECI");
            File AAC = new File(folder + "\\AAC");
            File TOIN = new File(folder + "\\TOIN");
            File LOIX = new File(folder + "\\LIOX");
            if (!ECI.exists()) {
                ECI.mkdir();
            }
            if (!AAC.exists()) {
                AAC.mkdir();
            }
            if (!TOIN.exists()) {
                TOIN.mkdir();
            }
            if (!LOIX.exists()) {
                LOIX.mkdir();
            }
            if (myesri == true) {
                files = new String[4];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[2] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[3] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                files = new String[6];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI_10.lpu";
                files[2] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[3] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[4] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX_10.lpu";
                files[5] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                File trgDir5 = new File(files[4]);
                File trgDir6 = new File(files[5]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                    FileUtils.copyFile(srcDir, trgDir5);
                    FileUtils.copyFile(srcDir, trgDir6);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            String logfile = folder + "\\" + path.substring(path.lastIndexOf("\\") + 1, path.lastIndexOf("."))
                    + ".log";
            log = new File(logfile);
            if (!log.exists()) {
                log.createNewFile();
            }

            for (int i = 0; i < files.length; i++) {
                int exitVal = 0;
                try {
                    String osName = System.getProperty("os.name");
                    String cmd = "cmd.exe /c " + passoloPath + " /openproject:" + files[i]
                            + " /runmacro=PslLpuSplitter_v3.bas" + " >> " + logfile;
                    System.out.println(cmd);
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(cmd);
                    exitVal = proc.waitFor();
                    System.out.println("Exit value: " + exitVal);
                    if (exitVal == 10) {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                File lpuFile = new File(files[i]);
                File logFile = new File(files[i].substring(0,
                        files[i].substring(0, files[i].lastIndexOf("\\")).lastIndexOf("\\") + 1)
                        + files[i].substring(files[i].lastIndexOf("\\") + 1, files[i].lastIndexOf("."))
                        + ".log");
                if (!lpuFile.exists()) {
                    logFile.delete();
                }
                File lpuFolder = new File(files[i].substring(0, files[i].lastIndexOf("\\")));
                if (lpuFolder.list().length == 0) {
                    lpuFolder.delete();
                }
            } // end for loop  
        }

    } catch (Exception e) {
        try {
            String content = e.getMessage();
            FileWriter fw = new FileWriter(log.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (Exception e1) {

        }

        return false;
    }
    return true;
}

From source file:ClassFinder.java

private static void findClassesInPathsDir(String strPathElement, File dir, Set<String> listClasses)
        throws IOException {
    String[] list = dir.list();
    for (int i = 0; i < list.length; i++) {
        File file = new File(dir, list[i]);
        if (file.isDirectory()) {
            // Recursive call
            findClassesInPathsDir(strPathElement, file, listClasses);
        } else if (list[i].endsWith(DOT_CLASS) && file.exists() && (file.length() != 0)) {
            final String path = file.getPath();
            listClasses.add(path.substring(strPathElement.length() + 1, path.lastIndexOf(".")) // $NON-NLS-1$
                    .replace(File.separator.charAt(0), '.')); // $NON-NLS-1$
        } else if (list[i].endsWith(DOT_JAR) && file.exists() && (file.length() != 0)) {
            ZipFile zipFile = new ZipFile(file);
            Enumeration entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                String strEntry = entries.nextElement().toString();
                if (strEntry.endsWith(DOT_CLASS)) {
                    listClasses.add(fixClassName(strEntry));
                }//from  w  w w  .j  a  v  a  2 s  .  com
            }
            zipFile.close();
        }
    }
}

From source file:com.hybris.datahub.outbound.utils.CommonUtils.java

/**
 * Read all the files in a folder/*from   www.  j  a  v  a  2 s  . c  o m*/
 *
 * @param filepath
 * @param character
 *           Fuzzy query based on the file name
 * @return key=filename<br/>
 *         value=filecontent
 */
public static Map<String, String> readFileContentOfTheFloder(final String filepath, final String character) {

    final File file = new File(filepath);
    final Map<String, String> result = new HashMap<String, String>();
    if (file.isDirectory()) {

        final String[] filelist = file.list();

        for (int i = 0; i < filelist.length; i++) {

            final File readfile = new File(filepath + File.separator + filelist[i]);
            if (!readfile.isDirectory()) {

                final String tempFilePath = readfile.getAbsolutePath();
                final String tempfileName = readfile.getName();

                if (StringUtils.isNotEmpty(character)) {
                    if (tempfileName.contains(character)) {
                        result.put(tempfileName, readFile(tempFilePath));
                    }
                } else {
                    result.put(tempfileName, readFile(tempFilePath));
                }
            }
        }
    }
    return result;
}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

private static void calculateFileCount(File file) {
    if (!file.isDirectory()) {
        fileCount++;/* w  ww .  j a  v  a  2s. c om*/
        return;
    }
    if (file.list() == null) {
        return;
    }
    for (String fileName : file.list()) {
        File f = new File(file.getAbsolutePath() + File.separator + fileName);
        calculateFileCount(f);
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Adds the folder to zip.//from   w ww .ja  va2  s.c o m
 * @param path the path
 * @param srcFolder the src folder
 * @param zip the zip
 */
private static void addFolderToZip(final String path, final String srcFolder, final ZipOutputStream zip) {

    final File folder = new File(srcFolder);

    final StringBuilder srcFolderPath = new StringBuilder();
    final StringBuilder pathString = new StringBuilder();

    for (final String fileName : folder.list()) {
        if (path.trim().isEmpty()) {
            addFileToZip(folder.getName(),
                    srcFolderPath.append(srcFolder).append(SLASH).append(fileName).toString(), zip);
        } else {
            addFileToZip(pathString.append(path).append(SLASH).append(folder.getName()).toString(),
                    srcFolderPath.append(srcFolder).append(SLASH).append(fileName).toString(), zip);
        }
        srcFolderPath.setLength(0);
        pathString.setLength(0);
    }
}

From source file:MyFormApp.java

public static 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;
            }/*  www  . j a  va 2 s  .  c  om*/
        }
    }
    return dir.delete();

}

From source file:fr.paris.lutece.plugins.jasper.web.JasperJspBean.java

public static boolean deleteFolderWithContent(File folder) {
    if (folder.isDirectory()) {
        String[] files = folder.list();

        for (int i = 0; i < files.length; i++) {
            boolean success = deleteFolderWithContent(new File(folder, files[i]));

            if (!success) {
                return false;
            }// w  w w. ja va 2  s . com
        }
    }

    return folder.delete();
}

From source file:gov.nasa.ensemble.dictionary.nddl.NDDLUtil.java

public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();//from w  ww .j a v  a 2  s.  co  m
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);
        IOUtils.copy(in, out);
        in.close();
        out.close();
    }
}