Example usage for java.io File canRead

List of usage examples for java.io File canRead

Introduction

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

Prototype

public boolean canRead() 

Source Link

Document

Tests whether the application can read the file denoted by this abstract pathname.

Usage

From source file:com.eryansky.common.web.utils.DownloadUtils.java

/**
 * //www. j  av  a2  s .  c om
 * @param request
 * @param response
 * @param filePath 
 * @param displayName ??
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);
    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();
    }
    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    download(request, response, new FileInputStream(file), displayName);
}

From source file:com.frostwire.android.gui.util.FileUtils.java

/** Given a folder path it'll return all the files contained within it and it's subfolders
 * as a flat set of Files./*from   ww w. ja  v a 2s. com*/
 * 
 * Non-recursive implementation, up to 20% faster in tests than recursive implementation. :)
 * 
 * @author gubatron
 * @param folder
 * @param extensions If you only need certain files filtered by their extensions, use this string array (without the "."). or set to null if you want all files. e.g. ["txt","jpg"] if you only want text files and jpegs.
 * 
 * @return The set of files.
 */
public static Collection<File> getAllFolderFiles(File folder, String[] extensions) {
    Set<File> results = new HashSet<File>();
    Stack<File> subFolders = new Stack<File>();
    File currentFolder = folder;
    while (currentFolder != null && currentFolder.isDirectory() && currentFolder.canRead()) {
        File[] fs = null;
        try {
            fs = currentFolder.listFiles();
        } catch (SecurityException e) {
        }

        if (fs != null && fs.length > 0) {
            for (File f : fs) {
                if (!f.isDirectory()) {
                    if (extensions == null || FilenameUtils.isExtension(f.getName(), extensions)) {
                        results.add(f);
                    }
                } else {
                    subFolders.push(f);
                }
            }
        }

        if (!subFolders.isEmpty()) {
            currentFolder = subFolders.pop();
        } else {
            currentFolder = null;
        }
    }
    return results;
}

From source file:Main.java

/**
 * Opens a {@link FileInputStream} for the specified file, providing better
 * error messages than simply calling <code>new FileInputStream(file)</code>.
 * <p/>//from  w w  w  . j av a  2s. c  om
 * At the end of the method either the stream will be successfully opened,
 * or an exception will have been thrown.
 * <p/>
 * An exception is thrown if the file does not exist.
 * An exception is thrown if the file object exists but is a directory.
 * An exception is thrown if the file exists but cannot be read.
 *
 * @param file the file to open for input, must not be {@code null}
 * @return a new {@link FileInputStream} for the specified file
 * @throws FileNotFoundException if the file does not exist
 * @throws IOException           if the file object is a directory
 * @throws IOException           if the file cannot be read
 * @since 1.3
 */
public static FileInputStream openInputStream(File file) throws IOException {
    if (file.exists()) {
        if (file.isDirectory()) {
            throw new IOException("File '" + file + "' exists but is a directory");
        }
        if (file.canRead() == false) {
            throw new IOException("File '" + file + "' cannot be read");
        }
    } else {
        throw new FileNotFoundException("File '" + file + "' does not exist");
    }
    return new FileInputStream(file);
}

From source file:com.dnielfe.manager.dialogs.DirectoryInfoDialog.java

private static String getFilePermissions(File file) {
    String per = "";

    per += file.isDirectory() ? "d" : "-";
    per += file.canRead() ? "r" : "-";
    per += file.canWrite() ? "w" : "-";
    per += file.canExecute() ? "x" : "-";

    return per;/*  w w  w .j  av  a2 s.  c  om*/
}

From source file:de.tudarmstadt.ukp.wikipedia.wikimachine.factory.SpringFactory.java

private static XmlBeanFactory getBeanFactory() {
    File outerContextFile = new File(OUTER_APPLICATION_CONTEXT);
    boolean outerContextFileProper = outerContextFile.exists() && outerContextFile.isFile()
            && outerContextFile.canRead();
    Resource res = (outerContextFileProper) ? new FileSystemResource(outerContextFile)
            : new ClassPathResource(INNER_APPLICATION_CONTEXT);
    return new XmlBeanFactory(res);
}

From source file:ucar.httpservices.CustomSSLProtocolSocketFactory.java

static KeyStore buildstore(String path, String password, String prefix) throws HTTPException {
    KeyStore store = null;//from www . j a v  a2  s.c o  m
    try {
        if (path != null && password != null) {
            File storefile = new File(path);
            if (!storefile.canRead())
                throw new HTTPException(
                        "Cannot read specified " + prefix + "store:" + storefile.getAbsolutePath());
            store = KeyStore.getInstance("JKS");
            InputStream is = null;
            try {
                is = new FileInputStream(storefile);
                store.load(is, password.toCharArray());
            } finally {
                if (is != null)
                    is.close();
            }
        }
    } catch (Exception e) {
        throw new HTTPException(e);
    }
    return store;
}

From source file:eu.eubrazilcc.lvl.core.conf.ConfigurationFinder.java

public final static boolean testConfigurationFiles(final String baseDir, final String[] filenames) {
    boolean passed = (isNotEmpty(baseDir) && filenames != null && filenames.length > 0 ? true : false);
    for (int i = 0; i < filenames.length && passed; i++) {
        try {//from   w ww. j av  a  2s  .c  om
            final File file = new File(concat(baseDir, filenames[i]));
            if (!file.isFile() || !file.canRead()) {
                passed = false;
            }
        } catch (Exception ignore) {
            passed = false;
        }
    }
    return passed;
}

From source file:android.databinding.tool.reflection.java.JavaAnalyzer.java

private static String loadAndroidHome() {
    Map<String, String> env = System.getenv();
    for (Map.Entry<String, String> entry : env.entrySet()) {
        L.d("%s %s", entry.getKey(), entry.getValue());
    }//from ww w .  jav a  2s.co m
    if (env.containsKey("ANDROID_HOME")) {
        return env.get("ANDROID_HOME");
    }
    // check for local.properties file
    File folder = new File(".").getAbsoluteFile();
    while (folder != null && folder.exists()) {
        File f = new File(folder, "local.properties");
        if (f.exists() && f.canRead()) {
            try {
                for (String line : FileUtils.readLines(f)) {
                    List<String> keyValue = Splitter.on('=').splitToList(line);
                    if (keyValue.size() == 2) {
                        String key = keyValue.get(0).trim();
                        if (key.equals("sdk.dir")) {
                            return keyValue.get(1).trim();
                        }
                    }
                }
            } catch (IOException ignored) {
            }
        }
        folder = folder.getParentFile();
    }

    return null;
}

From source file:uk.ac.sanger.cgp.wwdocker.actions.Utils.java

public static List<File> getWorkInis(BaseConfiguration config) {
    List<File> iniFiles = new ArrayList();
    Path dir = Paths.get(config.getString("wfl_inis"));
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path item : stream) {
            File file = item.toFile();
            if (file.isFile() && file.canRead() && !file.isHidden()) {
                if (!file.getName().endsWith(".ini")) {
                    continue;
                }/*  w w  w . ja v a 2s . c  o m*/
                iniFiles.add(file);
            }

        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return iniFiles;
}

From source file:com.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();/* w w w . j  a v  a 2s  . c  o  m*/
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}