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

private static void searchFiles(File root, List<File> files, String... extension) {
    if (root.isFile()) {
        for (String string : extension) {
            if (root.toString().endsWith(string)) {
                files.add(root);//  www  . j  a  v a  2 s  .  c o m
            }
        }
    } else {
        File[] children = root.listFiles();
        if (children != null) {
            for (File file : children) {
                searchFiles(file, files, extension);
            }
        }
    }
}

From source file:Main.java

public static Boolean checkFileExists(final String filePath) {
    File f = getFileObject(filePath);
    try {//from  w w w  . j ava2s  . c o m
        if (f != null && f.exists() && f.isFile()) {
            return true;
        } else {
            return false;
        }
    } finally {
        f = null;
    }
}

From source file:Main.java

public static String getFileMD5String(File file) {
    if (!file.exists() || file.isFile()) {
        return null;
    }/*from w ww. ja  v a  2 s  .co  m*/
    MessageDigest messagedigest;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
        messagedigest = MessageDigest.getInstance("MD5");
        int len;
        byte[] buf = new byte[1024];
        while ((len = fis.read(buf)) != -1) {
            messagedigest.update(buf, 0, len);
        }
        byte md5Bytes[] = messagedigest.digest();
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            stringbuffer.append(HEX_DIGITS[(md5Bytes[i] & 0xf0) >>> 4]);
            stringbuffer.append(HEX_DIGITS[md5Bytes[i] & 0x0f]);
        }
        return stringbuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);//  ww  w.j  av a2  s .c o  m
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

public static boolean renameDict(final File dictFile, final File newDictFile) {
    if (dictFile.isFile()) {
        return dictFile.renameTo(newDictFile);
    } else if (dictFile.isDirectory()) {
        final String dictName = dictFile.getName();
        final String newDictName = newDictFile.getName();
        if (newDictFile.exists()) {
            return false;
        }//from w ww. j  a  v  a 2s.  c  o m
        for (final File file : dictFile.listFiles()) {
            if (!file.isFile()) {
                continue;
            }
            final String fileName = file.getName();
            final String newFileName = fileName.replaceFirst(Pattern.quote(dictName),
                    Matcher.quoteReplacement(newDictName));
            if (!file.renameTo(new File(dictFile, newFileName))) {
                return false;
            }
        }
        return dictFile.renameTo(newDictFile);
    }
    return false;
}

From source file:Main.java

public static String getVideoChannelName() {
    File f = new File(CUST_PATH + VIDEO_CHANNEL_FILE_NAME);
    if (f.isFile()) {
        String result = null;/*  w  w w  .j  ava  2s.co m*/
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
            result = br.readLine();
        } catch (FileNotFoundException e) {
            //            Log.e(CHCCustomUtil.class.getName(), "Organization channel file not found!", e);
        } catch (Exception e) {
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (Exception e) {
                }
        }
        if (result != null)
            return result;
        else
            return new String();
    } else
        return new String();
}

From source file:Main.java

public static void delete(File file) {
    if (!file.exists()) {
        return;/*from w  ww  .  j  ava  2  s.  c  o  m*/
    }
    if (file.isFile()) {
        file.delete();
        return;
    }
    if (file.isDirectory()) {
        File[] childFiles = file.listFiles();
        if (childFiles == null || childFiles.length == 0) {
            file.delete();
            return;
        }

        for (int i = 0; i < childFiles.length; i++) {
            delete(childFiles[i]);
        }
        file.delete();
    }
}

From source file:Main.java

public static long getFileSize(String path) {
    if (TextUtils.isEmpty(path)) {
        return -1;
    }//ww w .ja  va2 s  . co  m

    File file = new File(path);
    return (file.exists() && file.isFile() ? file.length() : -1);
}

From source file:com.turn.ttorrent.cli.TrackerMain.java

private static void addAnnounce(Tracker tracker, File file, int depth) throws IOException, URISyntaxException {
    if (file.isFile()) {
        logger.info("Loading torrent from " + file.getName());
        Torrent torrent = new Torrent(file);
        tracker.announce(new TrackedTorrent(torrent.getName(), torrent.getInfoHash()));
        return;//from ww  w  .j  a va  2  s  . co m
    }
    if (depth > 3)
        return;
    FilenameFilter filter = new SuffixFileFilter(".torrent");
    for (File child : file.listFiles(filter))
        addAnnounce(tracker, child, depth + 1);
}

From source file:Main.java

public static byte[] readBytesFromFile(String path) {
    if (path == null || path.trim().length() == 0) {
        return null;
    }/*from www . j  a v  a  2s  . c  om*/

    File file = new File(path);

    if (file.exists() && file.isFile()) {
        int filelength = (int) file.length();
        byte[] filecontent = new byte[filelength];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();

        } catch (IOException e) {
            Log.v(TAG, "read file is error");
            return null;
        }
        return filecontent;
    }
    return null;
}