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

public static void deleteFile(File file) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();/* w ww . j  av  a 2s .  c o m*/
            }

            file.delete();
        }
    }
}

From source file:Main.java

/**
 *Valid plugin  md5/*from www.  j  av a2 s  .  c  o m*/
 * @param path   bundle archvie path
 * @param md5Sum   target  file md5
 * @return  if md5 matched,return true
 * ***/
public static boolean validFileMD5(String path, String md5Sum) {

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        File mFile = new File(path);
        if (mFile == null || !mFile.exists() || !mFile.isFile()) {
            return false;
        }
        FileInputStream in = new FileInputStream(mFile);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, mFile.length());
        messageDigest.update(byteBuffer);
        String digest = String.format("%032x", new BigInteger(1, messageDigest.digest()));
        return md5Sum.equals(digest.toString());
    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

    }
    return false;
}

From source file:Main.java

/**
 * Delete corresponding path, file or directory.
 *
 * @param file      path to delete.//from   w  w  w  .  j a  v a 2 s.c o  m
 * @param ignoreDir whether ignore directory. If true, all files will be deleted while directories is reserved.
 */
public static void delete(File file, boolean ignoreDir) {
    if (file == null || !file.exists()) {
        return;
    }
    if (file.isFile()) {
        file.delete();
        return;
    }

    File[] fileList = file.listFiles();
    if (fileList == null) {
        return;
    }

    for (File f : fileList) {
        delete(f, ignoreDir);
    }
    // delete the folder if need.
    if (!ignoreDir)
        file.delete();
}

From source file:com.github.jrh3k5.mojo.flume.io.ArchiveUtils.java

/**
 * Extract the contents of a TAR file./*from w  ww . j  av  a  2 s .com*/
 * 
 * @param tarFile
 *            A {@link File} representing the TAR file whose contents are to be extracted.
 * @param toDirectory
 *            A {@link File} representing the directory to which the contents of the TAR file to be extracted.
 * @throws IllegalArgumentException
 *             If the given TAR file is not a file or does not exist or the given output directory is not a directory or does not exist.
 * @throws IOException
 *             If any errors occur during the extraction.
 * @see #tarDirectory(File, File)
 */
public static void untarFile(File tarFile, File toDirectory) throws IOException {
    if (!tarFile.isFile()) {
        throw new IllegalArgumentException("TAR file " + tarFile + " must be an existent file.");
    }

    if (!toDirectory.exists()) {
        FileUtils.forceMkdir(toDirectory);
    }

    if (!toDirectory.isDirectory()) {
        throw new IllegalArgumentException(
                "Output directory " + toDirectory + " must be an existent directory.");
    }

    final TarUnArchiver unarchiver = new TarUnArchiver(tarFile);
    unarchiver.enableLogging(new Slf4jPlexusLogger(FlumeCopier.class));
    unarchiver.setDestDirectory(toDirectory);
    unarchiver.extract();
}

From source file:com.github.fi3te.iliasdownloader.controller.Util.java

public static void openFile(File file, Activity forMessages) {
    if (file != null && forMessages != null && file.isFile()) {
        String extension = FilenameUtils.getExtension(file.getPath());
        if (extension.length() > 0) {
            try {
                extension = extension.toLowerCase();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                        MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension));
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                forMessages.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {
                Toast.makeText(forMessages, forMessages.getResources().getString(R.string.unknown_type),
                        Toast.LENGTH_SHORT).show();
            }//from w ww.  j  a  v  a 2 s.c o  m
        }
    }
}

From source file:de.mendelson.comm.as2.AS2.java

public static void cleanup() {
    // RJC:  Runtime creates database, lock, and log artifacts.  During 
    // development time this is very annoying.  Temporarily cleaning
    // up these artifacts...
    // TODO better solution at some point (need persistent state)
    String RUNTIME_ARTIFACTS[] = new String[] { "AS2_DB_CONFIG.log", "AS2_DB_CONFIG.script",
            "AS2_DB_RUNTIME.log", "AS2_DB_RUNTIME.script", "client_server_session0.logd",
            "client_server_session1.logd", "client_server_session2.logd", "AS2_DB_CONFIG.properties",
            "AS2_DB_CONFIG.tmp", "AS2_DB_RUNTIME.properties", "AS2_DB_RUNTIME.tmp",
            "client_server_session0.log.lck", "client_server_session1.log.lck",
            "client_server_session2.log.lck", "mendelson_opensource_AS2.lock", "messages",
            "AS2_DB_CONFIG.properties", "AS2_DB_CONFIG.script", "AS2_DB_RUNTIME.properties",
            "AS2_DB_RUNTIME.script", "client_server_session0.log.lck", "client_server_session0.log" };

    for (String s : RUNTIME_ARTIFACTS) {
        File f = new File(s);
        if (f.isFile()) {
            f.delete();//from   w ww . jav  a2  s .  co  m
        } else {
            if (f.isDirectory()) {
                try {
                    FileUtils.deleteDirectory(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}

From source file:Main.java

public static boolean deleteFile(String path) {
    if (TextUtils.isEmpty(path)) {
        return true;
    }/* w  w  w .j  a v  a 2 s.  c om*/

    File file = new File(path);
    if (!file.exists()) {
        return true;
    }
    if (file.isFile()) {
        return file.delete();
    }
    if (!file.isDirectory()) {
        return false;
    }

    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return file.delete();
    }

    for (File f : files) {
        if (isFileExists(f)) {
            f.delete();
        } else if (f.isDirectory()) {
            deleteFile(f.getAbsolutePath());
        }
    }
    return file.delete();
}

From source file:Main.java

/**
 * in KB//ww  w  . j  a v  a  2 s  .  c  om
 *
 * @return KB
 */
public static long getCacheSize() {
    long sum = 0;
    if (filesInCache == null) {
        return 0;
    } else {
        for (File file : filesInCache) {

            if (!file.exists())
                continue;
            if (file.isFile()) {
                sum += file.length();
                //LogUtils.i(TAG, file.getAbsolutePath() + "isFile");
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (!files[i].exists())
                        continue;
                    if (files[i].isFile()) {
                        //LogUtils.i(TAG, file.getAbsolutePath() + "isDir");
                        sum += file.length();
                    }
                }
            }
        }
    }
    return sum / 1024;
}

From source file:com.voa.weixin.utils.WeixinUtils.java

public static Document getDocumentResource(String sourceName) throws Exception {
    String repertoryPath = Carp.ROOTPATH + sourceName;

    SAXReader reader = new SAXReader();
    Document doc = null;/*from  www.  j  a v a2  s  .c o m*/

    File repertoryFile = new File(repertoryPath);
    if (repertoryFile.exists() && repertoryFile.isFile()) {
        doc = reader.read(repertoryFile);
    } else {
        InputStream in = Carp.class.getResourceAsStream("/" + sourceName);
        doc = reader.read(in);
        in.close();
    }

    return doc;
}

From source file:com.ctriposs.rest4j.server.util.FileClassNameScanner.java

/**
 * Construct map from fully qualified class name to filename whose sources are found under a given source directory.
 * All source files are required to have an extension.
 *
 * @param sourceDir the source directory to scan
 * @param requiredExtension only include files whose extension equals to this parameter
 *                          null if no specific extension is required
 * @return map from fully qualified class name to filename for scanned source files.
 *///from w w w . jav  a 2  s  . c o  m
public static Map<String, String> scan(String sourceDir, String requiredExtension) {
    final String sourceDirWithSeparator = sourceDir.endsWith(File.separator) ? sourceDir
            : sourceDir + File.separator;
    final File dir = new File(sourceDirWithSeparator);
    if (!dir.exists() || !dir.isDirectory()) {
        return Collections.emptyMap();
    }

    // suppress the warning because of inconsistent FileUtils interface
    @SuppressWarnings("unchecked")
    final Collection<File> files = (Collection<File>) FileUtils.listFiles(dir, null, true);
    final Map<String, String> classFileNames = new HashMap<String, String>();
    final int prefixLength = sourceDirWithSeparator.length();
    for (File f : files) {
        assert (f.exists() && f.isFile());

        final int extensionIndex = f.getName().lastIndexOf('.');
        final String filePath = f.getPath();
        if (extensionIndex < 0 || !filePath.startsWith(sourceDirWithSeparator)) {
            continue;
        }

        final int reverseExtensionIndex = f.getName().length() - extensionIndex;
        final String classPathName = filePath.substring(prefixLength,
                filePath.length() - reverseExtensionIndex);
        if (classPathName.contains(".")) {
            // dot is not allowed in package name, thus not allowed in the directory path
            continue;
        }

        if (requiredExtension != null) {
            final String extension = f.getName().substring(extensionIndex + 1);
            if (!extension.equals(requiredExtension)) {
                continue;
            }
        }
        classFileNames.put(classPathName.replace(File.separator, "."), filePath);
    }

    return classFileNames;
}