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

/**
 * Read file//from  w  w w. jav a 2  s.  c  o m
 * 
 * @param filePath
 *            The path of the file
 * @return StringBuilder Return file content as StringBuffer, if the file
 *         doesn't exist return null
 */
public static StringBuilder readFile(String filePath) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (file != null && file.isFile()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (!fileContent.toString().equals("")) {
                    fileContent.append("\r\n");
                }
                fileContent.append(line);
            }
            reader.close();
            return fileContent;
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred. ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new RuntimeException("IOException occurred. ", e);
                }
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * read file to string list, a element of list is a line
 *
 * @param filePath/* w  w  w  .  j  av a  2 s  . c o m*/
 * @return if file not exist, return null, else return content of file
 * @throws IOException
 *             if an error occurs while operator BufferedReader
 */
public static List<String> readFileToList(String filePath) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (file == null || !file.isFile()) {
        return null;
    }

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:Main.java

/**
 * read file to string list, a element of list is a line
 * // ww  w  . j  a va 2s .  c o m
 * @param filePath
 * @param charsetName The name of a supported {@link java.nio.charset.Charset </code>charset<code>}
 * @return if file not exist, return null, else return content of file
 * @throws RuntimeException if an error occurs while operator BufferedReader
 */
public static List<String> readFileToList(String filePath, String charsetName) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (file == null || !file.isFile()) {
        return null;
    }

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:cf.janga.hook.utils.IOUtils.java

public static boolean hasExtension(File file, String extension) {
    String fileName = file.getName();
    return StringUtils.isNotBlank(fileName) && fileName.endsWith("." + extension) && file.isFile();
}

From source file:Main.java

/**
 * read file/*  w ww  .j  a  v  a  2  s.c om*/
 *
 * @param filePath
 * @return if file not exist, return null, else return content of file
 * @throws IOException
 *             if an error occurs while operator BufferedReader
 */
public static StringBuilder readFile(String filePath) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return null;
    }

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:eu.planets_project.services.utils.test.FileAccess.java

private static void index(File root, Map<String, File> map) {
    String[] list = root.list();//from   w w w .j  a v  a 2  s.co  m
    for (String name : list) {
        File f = new File(root, name);
        if (f.isDirectory() && !f.isHidden()) {
            index(f, map);
        } else if (f.isFile() && !f.isHidden()) {
            map.put(FilenameUtils.getExtension(f.getName()).toLowerCase(), f);
        }
    }
}

From source file:Main.java

/**
 * Create a file, If the file exists is not created.
 *
 * @param targetFile file./*from  www .j  a  v a 2s . c  o m*/
 * @return True: success, or false: failure.
 */
public static boolean createFile(File targetFile) {
    if (targetFile.exists()) {
        if (targetFile.isFile())
            return true;
        delFileOrFolder(targetFile);
    }
    try {
        return targetFile.createNewFile();
    } catch (IOException e) {
        return false;
    }
}

From source file:de.huxhorn.lilith.tools.CreateMd5Command.java

public static boolean createMd5(File input) {
    final Logger logger = LoggerFactory.getLogger(CreateMd5Command.class);

    if (!input.isFile()) {
        if (logger.isWarnEnabled())
            logger.warn("{} isn't a file!", input.getAbsolutePath());
        return false;
    }/* w ww . j  a  v  a 2s . c o  m*/
    File output = new File(input.getParentFile(), input.getName() + ".md5");

    try {

        FileInputStream fis = new FileInputStream(input);
        byte[] md5 = ApplicationPreferences.getMD5(fis);
        if (md5 == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Couldn't calculate checksum for {}!", input.getAbsolutePath());
            }
            return false;
        }
        FileOutputStream fos = new FileOutputStream(output);
        fos.write(md5);
        fos.close();
        if (logger.isInfoEnabled()) {
            logger.info("Wrote checksum of {} to {}.", input.getAbsolutePath(), output.getAbsolutePath());
            logger.info("MD5: {}", Hex.encodeHexString(md5));
        }
    } catch (IOException e) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while creating checksum!", e);
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Delete file or folder./*from w  ww. j  a  va 2  s .c om*/
 *
 * @param file file.
 * @return is succeed.
 * @see #delFileOrFolder(String)
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
public static boolean delFileOrFolder(File file) {
    if (file == null || !file.exists()) {
        // do nothing
    } else if (file.isFile())
        file.delete();
    else if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null)
            for (File sonFile : files)
                delFileOrFolder(sonFile);
        file.delete();
    }
    return true;
}

From source file:Main.java

public static boolean isRooted() {
    File file;
    boolean flag1 = false;
    for (String suSearchPath : suSearchPaths) {
        file = new File(suSearchPath + "su");
        if (file.isFile() && file.exists()) {
            flag1 = true;//from   ww w .  j a v  a  2s. co  m
            break;
        }
    }
    return flag1;
}