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

/**
 * Formats a file nice looking/*www  . j  a  v a  2s.c  o m*/
 * @param file The XML file to format
 * @throws Exception If the file isn't an XML file
 */
public static void format(File file) throws Exception {
    if (file.isFile()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;
        builder = factory.newDocumentBuilder();
        document = builder.parse(file);

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        Writer out = new FileWriter(file);
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);
        out.close();
    }
}

From source file:Main.java

public static boolean installApk(Context context, String filePath) {
    File file = new File(filePath);
    if (!file.exists() || !file.isFile() || file.length() <= 0) {
        return false;
    }/*from  w  w  w.j  ava 2 s  .  com*/
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
    return true;
}

From source file:Main.java

public static void traverseFolder(Map<String, File> fileMap, File rootFolder, File folderInCheck) {
    if (folderInCheck.isFile()) {
        int length = (int) rootFolder.getAbsolutePath().length();
        String pathKey = folderInCheck.getAbsolutePath().substring(length);
        fileMap.put(pathKey, folderInCheck);
        return;/*from   w ww .  ja  va 2 s . c  o m*/
    }

    String[] names = folderInCheck.list(new FilenameFilter() {
        // for filtering some files, such as ".xml"
        public boolean accept(File dir, String name) {
            return true;
        }
    });

    if (names == null || names.length == 0) {
        return;
    }

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

    for (File child : files) {
        traverseFolder(fileMap, rootFolder, child);
    }
}

From source file:Main.java

public static boolean isFileExists(File file) {
    return file != null && file.isFile() && file.exists();
}

From source file:Main.java

public static boolean deleteFile(final String fileName) {
    if (TextUtils.isEmpty(fileName)) {
        return false;
    }//from  w ww .  j ava  2 s .co  m

    boolean result = false;
    File file = new File(fileName);
    if (file.isFile() && file.exists()) {
        result = file.delete();
    }
    return result;
}

From source file:Main.java

public static double getSize(File file) {
    if (file.exists()) {
        if (!file.isFile()) {
            File[] fl = file.listFiles();
            double ss = 0;
            for (File f : fl) {
                ss += getSize(f);/*  w  ww.java  2 s .co m*/
            }
            return ss;
        } else {
            double ss = (double) file.length() / 1024 / 1024;
            return ss;
        }
    } else {
        return 0.0;
    }
}

From source file:Main.java

public static long folderSize(File directory, boolean rootMode) {
    long length = 0;
    for (File file : directory.listFiles()) {

        if (file.isFile())
            length += file.length();//from   ww w  .  ja v a2 s . com
        else
            length += folderSize(file, rootMode);
    }
    return length;
}

From source file:Main.java

public static boolean isFileExist(String fileName) {
    File file = new File(SDPATH + fileName);
    file.isFile();
    return file.exists();
}

From source file:Main.java

public static long getFileSize(String filepath) {
    if (TextUtils.isEmpty(filepath)) {
        return -1;
    }/*from  w w w.  j a v  a  2  s  .co m*/
    File file = new File(filepath);
    return (file.exists() && file.isFile() ? file.length() : -1);
}

From source file:Main.java

private static void getFiles(File aRootDir, Boolean aIsGetRelativePath, File aDirPath, boolean aIsContainSubDir,
        FilenameFilter aFilenameFilter, Collection<String> aOutList) {
    if (aOutList == null) {
        aOutList = new ArrayList<String>();
    }//from www. ja va  2  s. com

    File[] files = aDirPath.listFiles(aFilenameFilter);

    for (int i = 0; i < files.length; i++) {
        File f = files[i];
        if (f.isFile()) {
            String strPath = f.getPath();
            if (aRootDir != null && aIsGetRelativePath) {
                strPath = strPath.substring(aRootDir.getPath().length());
            }
            aOutList.add(strPath);
            if (!aIsContainSubDir)
                break;
        } else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
            getFiles(aRootDir, aIsGetRelativePath, f, aIsContainSubDir, aFilenameFilter, aOutList);
    }
}