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

/**
 * Tests if a folder is a RPG2k Game.//from   www. ja  va 2  s.  co m
 * (contains DATABASE_NAME and TREEMAP_NAME)
 *
 * @param dir Directory to test
 * @return true if RPG2k game
 */
public static boolean isRpg2kGame(File dir) {
    if (!dir.isDirectory() || !dir.canRead()) {
        return false;
    }

    boolean databaseFound = false;
    boolean treemapFound = false;

    for (File entry : dir.listFiles()) {
        if (entry.isFile() && entry.canRead()) {
            if (!databaseFound && entry.getName().equalsIgnoreCase(DATABASE_NAME)) {
                databaseFound = true;
            } else if (!treemapFound && entry.getName().equalsIgnoreCase(TREEMAP_NAME)) {
                treemapFound = true;
            }

            if (databaseFound && treemapFound) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

private static long getDirSize(File file) {
    long total = 0;
    if (file.exists()) {
        if (file.isFile()) {
            return file.length();
        } else if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                total += getDirSize(f);/*from w ww . ja  va2  s  .c  om*/
            }
        }
    }
    return total;
}

From source file:Main.java

public static long getFileSize(File file) {
    long size = 0;
    if (file.exists()) {
        if (file.isFile()) {
            size = file.length();//w w  w  .j a v  a  2 s .co  m
        } else if (file.isDirectory()) {
            for (File tmp : file.listFiles()) {
                size += getFileSize(tmp);
            }
        }
    }
    return size;
}

From source file:Main.java

public static String readFileByLines(@NonNull File file) {
    if (!file.isFile()) {
        return "";
    }//from w  w  w.  ja v a2s  . com
    BufferedReader reader = null;
    StringBuilder builder = new StringBuilder();
    try {
        reader = new BufferedReader(new FileReader(file));
        String tempString;
        while ((tempString = reader.readLine()) != null) {
            builder.append(tempString);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
    } finally {
        CloseableClose(reader);
    }

    return builder.toString();
}

From source file:Main.java

public static ArrayList<File> getFileList(File dir, String... fileExtensions) {
    final ArrayList<File> files = new ArrayList<File>();
    final File[] fileList = dir.listFiles();
    if (fileList != null) {
        for (File file : fileList) {
            if (file.isFile()) {
                if (fileExtensions.length == 0) {
                    files.add(file);/*from  w ww .j a v a  2 s. co  m*/
                } else {
                    String fileName = file.getName().toLowerCase();
                    for (String ext : fileExtensions) {
                        if (fileName.endsWith(ext)) {
                            files.add(file);
                            break;
                        }
                    }
                }
            } else {
                files.addAll(getFileList(file, fileExtensions));
            }
        }
    }
    return files;
}

From source file:Main.java

/**
 * Get folder size recursively// w  w w .  ja  v a2s .c  om
 * @param directory File
 * @return long
 */
public static long getFolderSizeRecursively(File directory) {
    long length = 0;
    for (File file : directory.listFiles()) {
        if (file.isFile()) {
            length += file.length();
        } else {
            length += getFolderSizeRecursively(file);
        }
    }
    return length;
}

From source file:Main.java

public static void zipChildren(File folder, String prefix, ZipOutputStream out) throws IOException {
    File[] files = folder.listFiles();
    if (files == null)
        return;/*from ww w.ja v  a2s.c  o  m*/
    for (File file : files) {
        if (file.isFile()) {
            String name = prefix + file.getName();
            ZipEntry entry = new ZipEntry(name);
            entry.setTime(file.lastModified());
            out.putNextEntry(entry);
            loadFromFile(file, out);
            out.closeEntry();
        } else if (file.isDirectory()) {
            zipChildren(file, prefix + file.getName() + "/", out);
        }
    }

}

From source file:Main.java

private static Map<Long, String> getFilePathAndModyTime(File file) {
    Map<Long, String> map = new HashMap<Long, String>();
    if (file.isFile()) {
        map.put(file.lastModified(), file.getAbsolutePath());
    } else if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            map.putAll(getFilePathAndModyTime(f));
        }//from w ww.  j  av  a 2s . c  o m
    }
    return map;
}

From source file:Main.java

public static byte[] readByteArray(File file) {
    if (!file.exists() || !file.isFile()) {
        return null;
    }/*from w ww  .j  av  a 2 s  . co  m*/
    byte[] data = null;
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(file);
        data = new byte[(int) file.length()];
        stream.read(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}

From source file:Main.java

/**
 * Get all temp files.//from w  w w  . ja va  2s .  c  om
 *
 * @return The list of existing temp files.
 */
public static File[] getTempCameraFiles() {
    File tempDir = getTempCameraFolder();

    File[] files = tempDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(@NonNull final File file) {
            return file.isFile();
        }
    });
    if (files == null) {
        files = new File[0];
    }
    Arrays.sort(files);

    return files;
}