java 文件操作工具类
import java.io.*;
import java.net.URLDecoder;
import java.util.logging.Logger;
public class FileUtils {
private static Logger LOG = Logger.getLogger("FileUtils");
public static String getBak(String path) {
return path + ".bak";
}
public static void renameFile(String oldname, String newname) {
if (!oldname.equals(newname)) {
File oldfile = new File(oldname);
File newfile = new File(newname);
if (!oldfile.exists()) {
return;
}
if (newfile.exists()) {
newfile.delete();
}
oldfile.renameTo(newfile);
} else {
LOG.info("新文件名和旧文件名相同...");
}
}
public static boolean createFile(String destFileName) {
File file = new File(destFileName);
if (file.exists()) {
LOG.info("创建单个文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
LOG.info("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
return false;
}
if (!file.getParentFile().exists()) {
LOG.info("目标文件所在目录不存在,准备创建它!");
if (!file.getParentFile().mkdirs()) {
LOG.info("创建目标文件所在目录失败!");
return false;
}
}
try {
if (file.createNewFile()) {
LOG.info("创建单个文件" + destFileName + "成功!");
return true;
} else {
LOG.info("创建单个文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
LOG.info("创建单个文件" + destFileName + "失败!" + e.getMessage());
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
LOG.info("创建目录" + destDirName + "失败,目标目录已经存在");
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
if (dir.mkdirs()) {
LOG.info("创建目录" + destDirName + "成功!");
return true;
} else {
LOG.info("创建目录" + destDirName + "失败!");
return false;
}
}
public static String createTempFile(String prefix, String suffix, String dirName) {
File tempFile = null;
if (dirName == null) {
try {
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
LOG.info("创建临时文件失败!" + e.getMessage());
return null;
}
} else {
File dir = new File(dirName);
if (!dir.exists()) {
if (!createDir(dirName)) {
LOG.info("创建临时文件失败,不能创建临时文件所在的目录!");
return null;
}
}
try {
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
LOG.info("创建临时文件失败!" + e.getMessage());
return null;
}
}
}
public static boolean copyFile(String srcFileName, String destFileName,
boolean overlay) {
File srcFile = new File(srcFileName);
if (!srcFile.exists()) {
return false;
} else if (!srcFile.isFile()) {
return false;
}
File destFile = new File(destFileName);
if (destFile.exists()) {
if (overlay) {
new File(destFileName).delete();
}
} else {
if (!destFile.getParentFile().exists()) {
if (!destFile.getParentFile().mkdirs()) {
return false;
}
}
}
int byteread = 0;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
while ((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
}
return true;
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean copyDirectory(String srcDirName, String destDirName,
boolean overlay) {
File srcDir = new File(srcDirName);
if (!srcDir.exists()) {
return false;
} else if (!srcDir.isDirectory()) {
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
File destDir = new File(destDirName);
if (destDir.exists()) {
if (overlay) {
new File(destDirName).delete();
} else {
return false;
}
} else {
System.out.println("目的目录不存在,准备创建。。。");
if (!destDir.mkdirs()) {
System.out.println("复制目录失败:创建目的目录失败!");
return false;
}
}
boolean flag = true;
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
flag = copyFile(files[i].getAbsolutePath(),
destDirName + files[i].getName(), overlay);
if (!flag)
break;
} else if (files[i].isDirectory()) {
flag = copyDirectory(files[i].getAbsolutePath(),
destDirName + files[i].getName(), overlay);
if (!flag)
break;
}
}
if (!flag) {
return false;
} else {
return true;
}
}
public static String getFileExt(String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
return suffix;
}
public static void main(String[] args) {
String dirName = "D:/work/temp/temp0/temp1";
FileUtils.createDir(dirName);
String fileName = dirName + "/temp2/tempFile.txt";
FileUtils.createFile(fileName);
String prefix = "temp";
String suffix = ".txt";
for (int i = 0; i < 10; i++) {
LOG.info("创建了临时文件:"
+ FileUtils.createTempFile(prefix, suffix, dirName));
}
for (int i = 0; i < 10; i++) {
LOG.info("在默认目录下创建了临时文件:"
+ FileUtils.createTempFile(prefix, suffix, null));
}
}
public static InputStream getResourceAsStream(String path) {
InputStream in = null;
try {
in = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}
public static File getResourceAsFile(String path) {
try {
return new File(URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource(path).getFile(), "utf-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}