file的后缀/创建,properties加载

package com.base.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class FileUtil {
    // private static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 构造函数私有化
     */
    private FileUtil() {
    }

    /**
     * 取得文件的扩展名,如:jpg
     *
     * @return
     */
    public static String getExtension(String fileName) {
        if (fileName == null || "".equals(fileName) || !fileName.contains(".")) {
            return "";
        }

        return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    }

    /**
     * 取得文件的扩展名,如:jpg
     *
     * @return
     */
    public static String getExtension(File file) {
        try {
            if (file == null || !file.isFile()) {
                return "";
            }

            return getExtension(file.getName());
        } catch (Exception e) {
            e.printStackTrace();

            return "";
        }
    }

    /**
     * 读取 properties 文件
     *
     * @param resourceFileName
     *            资源文件名
     * @return
     */
    public static Properties loadPropertiesFromResourceFile(
            String resourceFileName) {
        Properties properties = new Properties();

        InputStream in = null;

        try {
            in = FileUtil.class.getClassLoader().getResourceAsStream(
                    resourceFileName);

            properties.load(in);

            return properties;
        } catch (Exception e) {
            return properties;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 当不存在的时候创建目录结构,可创建多级目录结构 如果结构已经存在,则返回true createDirOnNotExists:
     * 2014年9月18日上午7:27:48
     *
     * @author
     * @param path
     * @return
     * @since JDK 1.6
     */
    public static boolean createDirOnNotExists(String path) {
        if (!isDirExists(path)) {
            File dir = new File(path);
            return dir.mkdirs();
        }
        return true;
    }

    /**
     * 判断目录是否存在 仅当该路径是目录并且存在的时候才返回true isDirExists: 2014年9月18日上午7:28:11
     *
     * @author
     * @param path
     * @return
     * @since JDK 1.6
     */
    public static boolean isDirExists(String path) {
        if (DataUtil.isEmpty(path)) {
            return false;
        }
        File dir = new File(path);
        return dir.isDirectory(); // 当存在并且是文件夹的时候返回
    }

    /**
     * 当不存在的时候创建文件 如果文件已经存在,则返回true createFileOnNotExists: 2014年9月18日上午7:51:01
     *
     * @author
     * @param path
     * @return
     * @since JDK 1.6
     */
    public static boolean createFileOnNotExists(String path) {
        if (!isFileExists(path)) {
            File file = new File(path);
            try {
                return file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    /**
     * 判断文件是否存在 仅当该路径是文件并且存在的时候才返回true
     *
     * @author
     * @param path
     * @return
     * @since JDK 1.6
     */
    public static boolean isFileExists(String path) {
        if (DataUtil.isEmpty(path)) {
            return false;
        }
        File file = new File(path);
        return file.isFile();
    }

    /**
     * 将字符串写入文件
     * @author
     * @date 2015年4月27日
     * @param content
     * @param path
     * @return
     */
    public static boolean writeFile(String content, String path) {
        File f = new File(path);// 新建一个文件对象
        FileWriter fw;
        try {
            fw = new FileWriter(f);// 新建一个FileWriter
            fw.write(content);// 将字符串写入到指定的路径下的文件中
            fw.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
  
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值