Java File Exist isFileExist(String fileNameAndPath)

Here you can find the source of isFileExist(String fileNameAndPath)

Description

is File Exist

License

Open Source License

Declaration

public static boolean isFileExist(String fileNameAndPath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static boolean isFileExist(String fileNameAndPath) {
        if (!isFileExistNotCreate(fileNameAndPath)) {
            createFolders(fileNameAndPath.replace(File.separator + getFileName(fileNameAndPath), ""));
            return newFile(fileNameAndPath, "");
        }//from   w  w w .  j a va 2  s .  c o m
        return true;
    }

    /**
     * check if the specified file exists
     *
     * @param fileNameAndPath
     *       the name of the file to be checked
     * @return boolean true if exits, false if not
     */
    public static boolean isFileExistNotCreate(String fileNameAndPath) {
        return new File(fileNameAndPath).isFile();
    }

    /**
     * create multi-level directory <br>
     * Example use: createFolders("/", "Users", "Power", "Main");
     *
     * @param paths
     *       must be at least 1 element is root folder
     *       (Optional) the folder that want to created.
     * @return String the created directory path or empty string if something error
     */
    public static String createFolders(String... paths) {
        if (paths.length == 0)
            return "";
        String root = paths[0];
        String[] other = new String[paths.length - 1];
        System.arraycopy(paths, 1, other, 0, paths.length - 1);

        try {
            File file = new File(root);
            if (!file.exists())
                file.mkdirs();
            if (!file.isDirectory()) {
                System.err.println("folderPath must be directory");
                return "";
            }

            String nextPath = root;
            for (String path : other) {
                if (nextPath.lastIndexOf(File.separator) != 0) {
                    nextPath += File.separator;
                }
                newFolder(nextPath + path);
                nextPath = nextPath + path;
            }
            return nextPath;
        } catch (Exception e) {
            System.out.println("create multi-level directory failed");
            e.printStackTrace();
        }
        return "";
    }

    /**
     * get file name from path
     *
     * @param path
     *       absolute file/folder path
     * @return name of the file
     */
    public static String getFileName(String path) {
        return new File(path).getName();
    }

    /**
     * create new file
     *
     * @param filePathAndName
     *       String file path and name
     * @param fileContent
     *       String file content
     * @return boolean flag to indicate create success or not
     */
    public static boolean newFile(String filePathAndName, String fileContent) {
        return newFile(filePathAndName, fileContent, false);
    }

    /**
     * create file
     *
     * @param filePathAndName
     *       String file path and name
     * @param fileContent
     *       String file content
     * @param append
     *       true to append, false to create
     * @return boolean append to indicate create success or not
     */
    public static boolean newFile(String filePathAndName, String fileContent, boolean append) {
        try {
            File file = new File(filePathAndName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file, append);
            PrintWriter printWriter = new PrintWriter(fileWriter);
            printWriter.println(fileContent);

            printWriter.close();
            fileWriter.close();
            return true;
        } catch (Exception e) {
            System.out.println("create file failed");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * create file with specified encoding
     *
     * @param filePathAndName
     *       String file path and name
     * @param fileContent
     *       String file content
     * @param encoding
     *       the specified encoding, such as GBK or UTF-8
     * @return boolean flag to indicate create success or not
     */
    public static boolean newFile(String filePathAndName, String fileContent, String encoding) {
        try {
            File file = new File(filePathAndName);
            if (!file.exists()) {
                file.createNewFile();
            }
            PrintWriter printWriter = new PrintWriter(file, encoding);
            printWriter.println(fileContent);
            printWriter.close();
            return true;
        } catch (Exception e) {
            System.out.println("create file failed");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * check is path a directory
     *
     * @param path
     *       absolute folder path
     * @return true if path is directory path
     */
    public static boolean isDirectory(String path) {
        return new File(path).isDirectory();
    }

    /**
     * create folder
     *
     * @param folderPath
     *       String folder path
     * @return true, if successful create new folder; otherwise will return false.
     */
    public static boolean newFolder(String folderPath) {
        try {
            File myFilePath = new File(folderPath);
            if (!myFilePath.exists()) {
                return myFilePath.mkdir();
            }
            return false;
        } catch (Exception e) {
            System.out.println("create folder failed");
            e.printStackTrace();
            return false;
        }
    }
}

Related

  1. isFileExist(File file)
  2. isFileExist(File file, File folder)
  3. isFileExist(final String filePathString)
  4. isFileExist(String assetDestinationLocation, String id, String assetId)
  5. isFileExist(String file)
  6. isFileExist(String filePath)
  7. isFileExist(String filePath)
  8. isFileExist(String path)
  9. isFileExist(String sFileName)