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) {
}
}
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) {
}
}