Example usage for java.io File isFile

List of usage examples for java.io File isFile

Introduction

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

Prototype

public boolean isFile() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a normal file.

Usage

From source file:Main.java

/**
 * read file//from www . ja va  2s  . c  o m
 *
 * @param charsetName The name of a supported {@link java.nio.charset.Charset
 *                    </code>charset<code>}
 * @return if file not exist, return null, else return content of file
 * @throws RuntimeException if an error occurs while operator BufferedReader
 */
public static String readFile(File file, String charsetName) {
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return fileContent.toString();
    }

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        reader.close();
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
    return fileContent.toString();
}

From source file:Main.java

public static StringBuilder readFile(File file, String charsetName) {
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return null;
    }/*from w w  w  .j  a  v  a  2s . c  om*/
    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        closeIO(reader);
    }
}

From source file:downloadwebpages.DownloadWebpages.java

/**
 * check that the required files and folders exist on the harddrive
 * @return true if all exist//from w  ww .  j  a va  2 s.  c  o  m
 */
private static boolean checkFilesExist() {
    boolean exist = true;
    System.out.println("Searching " + new File("").getAbsolutePath());

    // check html template
    File h = new File(htmlTemplatePath);
    if (h.isFile()) {
        System.out.println("Found: " + htmlTemplatePath);
    } else {
        System.out.println("ERROR Missing File: " + htmlTemplatePath);
        exist = false;
    }

    // check webpages text file
    File w = new File(webpagesTxtFilePath);
    if (w.isFile()) {
        System.out.println("Found: " + webpagesTxtFilePath);
    } else {
        System.out.println("ERROR Missing File: " + webpagesTxtFilePath);
        exist = false;
    }

    // check data folder
    File d = new File(dataFolderPath);
    if (d.isDirectory()) {
        System.out.println("Found: " + dataFolderPath);
    } else {
        System.out.println("ERROR Missing Folder: " + dataFolderPath);
        exist = false;
    }

    return exist;
}

From source file:Main.java

public static byte[] readContentBytesFromFile(File fileForRead) {
    if (fileForRead == null) {
        return null;
    } else if (fileForRead.exists() && fileForRead.isFile()) {
        ReentrantReadWriteLock.ReadLock readLock = getLock(fileForRead.getAbsolutePath()).readLock();
        readLock.lock();/*from  ww w. j a  v a2s  .c o m*/
        Object data = null;
        BufferedInputStream input = null;

        try {
            byte[] data1 = new byte[(int) fileForRead.length()];
            int e = 0;
            input = new BufferedInputStream(new FileInputStream(fileForRead), 8192);

            while (e < data1.length) {
                int bytesRemaining = data1.length - e;
                int bytesRead = input.read(data1, e, bytesRemaining);
                if (bytesRead > 0) {
                    e += bytesRead;
                }
            }

            byte[] bytesRemaining1 = data1;
            return bytesRemaining1;
        } catch (IOException var10) {
        } finally {
            closeQuietly(input);
            readLock.unlock();
        }

        return null;
    } else {
        return null;
    }
}

From source file:com.plugin.excel.xsd.node.store.impl.FileHelper.java

public static void findFiles(File directory, List<File> files) {
    // get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            files.add(file);//from w w w. jav  a2  s . co  m
        } else if (file.isDirectory()) {
            findFiles(file.getAbsolutePath(), files);
        }
    }
}

From source file:com.plugin.excel.xsd.node.store.impl.FileHelper.java

public static void findFiles(String directoryName, List<File> files) {
    File directory = new File(directoryName);
    // get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            files.add(file);//from  w w  w.  j ava  2 s  .  co  m
        } else if (file.isDirectory()) {
            findFiles(file.getAbsolutePath(), files);
        }
    }
}

From source file:org.crazydog.util.spring.FileSystemUtils.java

/**
 * Recursively copy the contents of the {@code src} file/directory
 * to the {@code dest} file/directory./*from ww  w  . jav a2s  .c  o  m*/
 *
 * @param src  the source directory
 * @param dest the destination directory
 * @throws IOException in the case of I/O errors
 */
public static void copyRecursively(File src, File dest) throws IOException {
    org.springframework.util.Assert.isTrue(src != null && (src.isDirectory() || src.isFile()),
            "Source File must denote a directory or file");
    org.springframework.util.Assert.notNull(dest, "Destination File must not be null");
    doCopyRecursively(src, dest);
}

From source file:it.geosolutions.tools.io.file.reader.TextReader.java

/**
 * Get the contents of a {@link File} as a String using the specified character encoding.
 * /*from w  ww.ja  v a2  s.  c o  m*/
 * @param file
 *            {@link File} to read from
 * @param encoding
 *            IANA encoding
 * @return a {@link String} containig the content of the {@link File} or
 *         <code>null<code> if an error happens.
 */
public static String toString(final File file, final String encoding) {
    Objects.notNull(file);
    if (!file.isFile() || !file.canRead() || !file.exists())
        return null;
    InputStream stream = null;
    try {
        if (encoding == null)
            return org.apache.commons.io.IOUtils.toString(new FileInputStream(file));
        else
            return org.apache.commons.io.IOUtils.toString(new FileInputStream(file), encoding);
    } catch (Throwable e) {
        if (LOGGER.isWarnEnabled())
            LOGGER.warn(e.getLocalizedMessage(), e);
        return null;
    } finally {
        if (stream != null)
            try {
                stream.close();
            } catch (Throwable e) {
                if (LOGGER.isTraceEnabled())
                    LOGGER.trace(e.getLocalizedMessage(), e);
            }
    }
}

From source file:Main.java

public static void deletFiles(File delFile, Context mContext) {
    // TODO Auto-generated method stub
    if (delFile == null) {
        return;/*from   ww  w.  j a  v a  2 s .  c om*/
    }

    if (delFile.exists()) {
        if (delFile.isFile()) {
            delFile.delete();
            deleteFileForMediaStore(mContext, delFile);
        }
    }
}

From source file:com.predic8.membrane.examples.config.ConfigSerializationTest.java

private static void recurse(File file, ArrayList<Object[]> res) {
    OUTER: for (File f : file.listFiles()) {
        if (f.isDirectory())
            recurse(f, res);/*from  ww w  . j  av  a 2  s  . com*/
        if (f.isFile() && f.getName().equals("proxies.xml")) {
            String path = f.getAbsolutePath();
            for (String exclude : EXCLUDED)
                if (path.contains(File.separator + exclude + File.separator))
                    continue OUTER;
            res.add(new Object[] { path });
        }
    }
}