本篇带来关于文件操作的简单工具类,包括文件夹创建,文件夹删除,文件创建,文件重命名,文件复制,文件删除。如果需要文件夹复制,其实就是创建文件夹和复制文件的操作。下面直接上代码
package com.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件操作工具类,提供文件夹创建 、文件夹删除、文件创建、文件删除、文件重命名、文件复制等功能
*/
public class FileControlUnitl {
/**
* 创建文件夹
* @param url 创建的文件夹路径,例如:E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file,其中,file就是将要创建的文件夹名字
* @return boolean 返回创建成功(true)或者失败(false)
* @throws Exception
*/
public static boolean createFolder(String url) throws Exception{
url = url.replace("\\", "/");
File folder = new File(url);
if(folder.isDirectory() || folder.exists()){
throw new IOException("创建文件夹失败:["+folder+"]文件夹已经存在!");
}
if(!folder.isFile()){
folder.mkdirs();
}
//检测文件夹是否创建成功
if(folder.isDirectory() && folder.exists()){
return true;
}else {
return false;
}
}
/**
* 根据文件夹名及其所在路径删除文件夹
* @param url 文件及其所在路径,例如::E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file
* @return
* @throws IOException
*/
public static boolean deleteFolder(String url) throws IOException{
url = url.replace("\\", "/");
File folder = new File(url);
//判断是否为目录
if(!folder.isDirectory()){
throw new IOException("文件夹删除失败:["+folder+"]不是目录!");
}
if(!folder.exists()){
throw new IOException("文件夹删除失败:["+folder+"]文件夹不存在!");
}
//判断文件是否存在文件,存在文件不允许删除
if(folder.listFiles().length>0){
throw new IOException("文件夹删除失败:["+folder+"]目录下面存在文件或文件夹!");
}
//删除文件夹
folder.delete();
//返回文件夹删除是否成功
if(folder.exists()){
return false;
}else{
return true;
}
}
/**
* 根据路径和文件名创建文件
* @param url 创建文件的路径,例如:E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file
* @param name 文件名 例如:testfile.java
* @return boolean 返回创建成功(true)或者失败(false)
* @throws Exception
*/
public static boolean createFile(String url,String name) throws Exception{
url = url.replace("\\", "/");
File folder = new File(url);
//判断是否是目录
if(!folder.isDirectory()){
throw new IOException("创建文件失败:["+folder+"]不是文件夹路径!");
}
//判断文件是否已经存在
url = url + "/" + name;
File file = new File(url);
if(file.exists()){
throw new IOException("创建文件失败:["+name+"]文件已经存在!");
}
//创建文件
file.createNewFile();
//检测文件是否创建成功
if(file.exists() && file.isFile()){
return true;
}else {
return false;
}
}
/**
* 根据旧文件地址和文件名,重命名文件名
* @param url 旧文件地址以及文件名,如:E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file\\TestFile.java
* @param newname 新文件名:New_TestFile.java
* @return
* @throws IOException
*/
public static boolean reanmeFile(String url,String newname) throws IOException{
String old_url = url.replace("\\", "/");
File old_file = new File(old_url);
if(!old_file.isFile()||!old_file.exists()){
throw new IOException("文件重命名失败:["+old_file+"]文件不存在!");
}
//获取旧的文件名
String old_name = old_file.getName();
//获取父文件路径
String parent_url = old_file.getParent();
//重命名
String new_url = parent_url + "/" + newname;
File new_file = new File(new_url);
old_file.renameTo(new_file);
//校验文件是否重命名成功
String new_name = new_file.getName();
if(old_name.equals(new_name)){
return false;
}else{
return true;
}
}
/**
* 根据源文件和复制路径,复制文件
* @param sourceurl 源文件及其路径 如:E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file\\TestFile.java
* @param copyurl 复制文件路径 如:E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file\\copyfile
* @return
* @throws Exception
*/
public static boolean copyFile(String sourceurl,String copyurl) throws Exception{
sourceurl = sourceurl.replace("\\", "/");
copyurl = copyurl.replace("\\", "/");
File source_file = new File(sourceurl);
File copy_file = new File(copyurl);
//判断文件以及复制路径是否存在
if(!source_file.exists()){
throw new IOException("文件复制失败:源文件["+source_file+"]不存在");
}
if(!copy_file.isDirectory()){
throw new IOException("文件复制失败:复制路径["+copyurl+"]错误");
}
// File parent = copy_file.getParentFile();
// //创建复制路径
// if(!parent.exists()){
// parent.mkdirs();
// }
//创建复制文件
copyurl = copyurl + "/" + source_file.getName();
copy_file = new File(copyurl);
if(!copy_file.exists()){
copy_file.createNewFile();
}
FileInputStream fis = new FileInputStream(source_file);
FileOutputStream fos = new FileOutputStream(copy_file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] kb = new byte[1024];
int index;
while((index = bis.read(kb))!=-1){
bos.write(kb, 0, index);
}
bos.close();
bis.close();
fos.close();
fis.close();
//判断文件是否复制成功
if(!copy_file.exists()){
return false;
}else if(source_file.length()!= copy_file.length()){
return false;
}else{
return true;
}
}
/**
* 根据文件及其所在路径删除文件
* @param url 文件及其所在路径,例如::E:\\Workspaces\\MyEclipse 10\\test\\src\\com\\file\\TestFile.java
* @return
* @throws IOException
*/
public static boolean deleteFile(String url) throws IOException{
url = url.replace("\\", "/");
File file = new File(url);
//判断是否为文件
if(!file.isFile()){
throw new IOException("文件删除失败:["+file+"]不是文件!");
}
//判断文件是否存在
if(!file.exists()){
throw new IOException("文件删除失败:["+file+"]文件不存在!");
}
//删除文件
file.delete();
//返回文件删除是否成功
if(file.exists()){
return false;
}else{
return true;
}
}
}
大致的文件操作流程就是这样,至于细节,入参形式,校验方式,返回类型等问题可以根据需要调整完善。