/**
 * 创建一个文件 如果存在,不做任何操作.如果不存在创建文件夹
 * 
 * @param filePath
 *            文件路径
 * @throws IOException
 *             IO异常
 */
public static void newFile(String filePath) throws IOException {
// 取得最后一个分隔符的位置
int position = filePath.lastIndexOf("\\");
// 取得文件夹路径
String folderPath = filePath.substring(0, position);
newFolder(folderPath);
// 实例化File类
File file = new File(filePath);
file.createNewFile();
} /**
 * 删除一个文件
 * 
 * @param filePath
 *            文件路径
 */
public static void deleteFile(String filePath) {
// 实例化File类
File file = new File(filePath);
if (!file.isDirectory()) {
file.delete();
}
} /**
 * 文件拷贝
 * 
 * @param srcPath
 *            源文件
 * @param destPath
 *            目标文件
 * @throws IOException
 *             IO异常
 */
public static void copyFile(String srcPath, String destPath)
throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 打开输入流
fis = new FileInputStream(new File(srcPath));
// 打开输出流
fos = new FileOutputStream(new File(destPath));
// 用输入流中的方法读文件
int fisContent = fis.read();
// 用输出流的方法写文件
if (fisContent != -1) {
fos.write(fisContent);
fisContent = fis.read();
}
// 把输出流中的东西强制刷到destPath
fos.flush();
} finally {
// 倒叙关闭输入流、输出流
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
}
} /**
 * 文件拷贝(缓冲区实现)
 * 
 * @param srcPath
 *            源文件
 * @param destPath
 *            目标文件
 * @throws IOException
 *             IO异常
 */
public static void copyFileByBuffered(String srcPath, String destPath)
throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建字节缓存输入流
bis = new BufferedInputStream(
new FileInputStream(new File(srcPath)));
// 创建字节缓存输出流
bos = new BufferedOutputStream(new FileOutputStream(new File(
destPath)));
// 用缓存输入流读
byte[] buffer = new byte[SIZE];
int len = bis.read();
while (len > 0) {
// 用缓存输入流写
bos.write(buffer, 0, len);
// 继续读
len = bis.read();
}
} finally {
// 倒叙关闭缓存输入、输出流
if (bos != null) {
bos.close();
}
if (bis != null) {
bis.close();
}
}
} /**
 * 文件剪切
 * 
 * @param srcPath
 *            源文件
 * @param destPath
 *            目标文件
 * @throws IOException
 *             IO异常
 */
public static void cutFileByBuffered(String srcPath, String destPath)
throws IOException {
// 文件拷贝
copyFileByBuffered(srcPath, destPath);
// 文件删除
deleteFile(srcPath);
} /**
 * 创建文件夹
 * 
 * @param filePath
 *            文件路径
 */
public static void newFolder(String folderPath) {
// 实例化File
File folder = new File(folderPath);
// 判断文件夹是否存在,
if (!folder.exists()) {
// 创建文件夹的方法
folder.mkdirs();
} } /**
 * 文件夹拷贝
 * 
 * @param srcFolderPath
 *            源文件夹
 * @param destFolderPath
 *            目标文件夹
 * @throws IOException
 *             IO异常
 */
public static void copyFolder(String srcFolderPath, String destFolderPath)
throws IOException {
String subSrcFileName = "";
File subFile = null;
String subDestFileName = ""; // 判断源文件夹是否存在
File srcFolder = new File(srcFolderPath);
if (!srcFolder.isDirectory()) {
throw new IOException("对不起!该文件夹不存在");
}
// 创建目标文件夹
File destFolder = new File(destFolderPath);
destFolder.mkdirs();
// 拷贝动作
// 取得源文件夹路径下的所有文件
String[] srcFile = srcFolder.list();
for (String srcFileName : srcFile) {
// 拼出完整路径(源文件)
subSrcFileName = srcFolder.getAbsolutePath() + File.separator
+ srcFileName;
subFile = new File(subSrcFileName);
// 拼出完整的目标路径
subDestFileName = destFolder.getAbsolutePath() + File.separator
+ srcFileName;
if (subFile.isDirectory()) {
// 文件夹的拷贝
copyFolder(subSrcFileName, subDestFileName);
} else {
// 非文件夹的拷贝
copyFileByBuffered(subSrcFileName, subDestFileName);
}
}
} /**
 * 文件夹删除
 * 
 * @param srcPath
 *            源文件夹路径
 * @throws IOException
 *             IO异常
 */
public static void deleteFolder(String srcFolderPath) throws IOException {
String subFilepath = "";
File subFile = null;
// 对于文件夹路径实例化
File srcFolder = new File(srcFolderPath);
// 文件夹不存在
if (!srcFolder.exists()) {
return;
}
// 取得文件夹路径下的所有文件
String[] subSrcFile = srcFolder.list();
for (String subFileName : subSrcFile) {
// 取得指定路径下的文件的绝对路径
subFilepath = srcFolder.getAbsolutePath() + File.separator
+ subFileName;
// 对指定文件实例化
subFile = new File(subFilepath);
if (subFile.isDirectory()) {
// 把源文件夹删除(文件夹不空)
deleteFolder(subFilepath);
} else {
subFile.delete();
}
}
srcFolder.delete();
}