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

public static String readFromFile(String file) {
    try {/*from  ww w .  ja va  2  s  . c  om*/
        File f = new File(file);

        if (!f.exists() || !f.canRead()) {
            return null;
        }

        BufferedInputStream ipt = new BufferedInputStream(new FileInputStream(f));

        byte[] buf = new byte[512];
        StringBuilder sb = new StringBuilder();

        while (ipt.available() > 0) {
            int len = ipt.read(buf, 0, 512);
            sb.append(new String(buf, 0, len, "UTF-8"));
        }

        ipt.close();
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String getFile(final String filename) {
    String s = "";
    final File f = new File(filename);

    if (f.exists() && f.canRead()) {
        try {// ww  w  . ja  va 2 s  . co m
            final BufferedReader br = new BufferedReader(new FileReader(f), 256);
            String buffer = null;
            while ((buffer = br.readLine()) != null) {
                s += buffer + "\n";
            }

            br.close();
        } catch (final Exception e) {
            Log.e(TAG, "Error reading file: " + filename, e);
            s = null;
        }
    }
    return s;
}

From source file:io.github.kitarek.elasthttpd.plugins.consumers.file.mapper.UriToFileMapper.java

private static boolean isValidExistingAbsoluteDirectory(File f) {
    return f.canRead() && f.exists() && f.isAbsolute() && f.isDirectory();
}

From source file:MakeDirectories.java

private static void fileData(File f) {
    System.out.println("Absolute path: " + f.getAbsolutePath() + "\n Can read: " + f.canRead()
            + "\n Can write: " + f.canWrite() + "\n getName: " + f.getName() + "\n getParent: " + f.getParent()
            + "\n getPath: " + f.getPath() + "\n length: " + f.length() + "\n lastModified: "
            + f.lastModified());//from w  w w  .  ja v  a  2  s.co m
    if (f.isFile())
        System.out.println("It's a file");
    else if (f.isDirectory())
        System.out.println("It's a directory");
}

From source file:Main.java

public static File[] listBackups(File backupPath) {

    if (!backupPath.isDirectory() || !backupPath.canRead()) {
        return new File[0];
    }/*from ww  w  .j  a  v a  2 s.  c o  m*/
    File[] files = backupPath.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return (new File(dir, filename)).isFile() && filename.endsWith(".backup");
        }
    });

    if (files != null) {
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File s1, File s2) {
                return s2.getName().compareTo(s1.getName());
            }
        });
        return files;
    } else {
        return new File[0];
    }
}

From source file:Main.java

public static NodeList getNodesByXPath(File file, String xpath) {
    if (!file.isFile() || !file.canRead())
        return null;

    Document document;/* ww  w  .  j  a  v a 2s  .com*/
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    if (document == null) {
        return null;
    }

    Object result;
    try {
        XPath xpathCompiler = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpr = xpathCompiler.compile(xpath);
        result = xPathExpr.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }

    return (NodeList) result;
}

From source file:Main.java

public static void init(Context context) {
    voice_wechat_paths = new ArrayList<>();
    voice_qq_paths = new ArrayList<>();

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File dir = Environment.getExternalStorageDirectory();
        File f = new File(dir + "/tencent/MicroMsg");

        if (f.exists() && f.canRead() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files == null || files.length == 0) {
                return;
            }/*from  w w  w.j  av  a 2s .  c  om*/

            for (File f0 : files) {
                if (f0.isDirectory() && f0.getName().length() > 24) {
                    voice_wechat_paths.add(f0.getAbsolutePath() + "/voice2");
                }
            }
        }

        File exportDir = new File(dir, "silkv3_mp3");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }

        export_dir = exportDir.getAbsolutePath();
    }
}

From source file:Main.java

private static void readGpxDirectory(File dir, final Map<String, Long> map, String parent,
        boolean absolutePath) {
    if (dir != null && dir.canRead()) {
        File[] files = dir.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.getName().toLowerCase().endsWith(".gpx")) { //$NON-NLS-1$
                    map.put(absolutePath ? f.getAbsolutePath() : parent + f.getName(), f.lastModified());
                } else if (f.isDirectory()) {
                    readGpxDirectory(f, map, parent + f.getName() + "/", absolutePath);
                }//from   w w w .  j  a v  a 2s . co  m
            }
        }
    }
}

From source file:Main.java

/**
 * Determine whether a file is a ZIP File.
 */// www  . j  a va  2s  .c  om
public static boolean isZipFile(File file) throws IOException {
    if (file.isDirectory()) {
        return false;
    }
    if (!file.canRead()) {
        throw new IOException("Cannot read file " + file.getAbsolutePath());
    }
    if (file.length() < 4) {
        return false;
    }
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
    int test = in.readInt();
    in.close();
    return test == 0x504b0304;
}

From source file:Main.java

public static String extSD() {
    String externalsd = "";

    if (!TextUtils.isEmpty(System.getenv("SECONDARY_STORAGE"))) {
        final String externalstorage[] = System.getenv("SECONDARY_STORAGE").split(":");
        for (final String dirs : externalstorage) {
            final File dir = new File(dirs);
            if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
                externalsd = dirs;/*ww  w  .  ja  v  a2s. c  o m*/
                break;
            }
        }
    } else {
        final String supported[] = { "/mnt/extSdCard", "/storage/sdcard1", "/mnt/external_sd" };
        for (final String dirs : supported) {
            final File dir = new File(dirs);
            if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
                externalsd = dirs;
                break;
            }
        }
    }
    return externalsd;
}