Java文件移动,复制,删除
1、拷贝文件,文件夹

2、拷贝文件核心处理方法

3、删除文件夹,文件

4、移动文件,文件夹(调用的是复制和删除方法)
/**
* 移动文件
* @param filePath 文件路径 -- 从哪里移动
* @param destPath 目标路径 -- 移动到哪里
*/
public static void moveFile(String filePath, String destPath){
saveAsFile(filePath, destPath);//拷贝
deleteFile(filePath);//删除
}
/**
* 移动文件
* @param file 文件对象 -- 从那里移动
* @param destPath 目标路径 -- 移动到哪里
*/
public static void moveFile(File file, String destPath){
try {
saveAsFile(file, destPath); //拷贝
deleteFile(file);//删除
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
5、/**
* 移动文件
* @param filePath 文件路径 -- 从哪里移动
* @param destPath 目标路径 -- 移动到哪里
*/
public static void moveFile(String filePath, String destPath){
saveAsFile(filePath, destPath);//拷贝
deleteFile(filePath);//删除
}
/**
* 移动文件
* @param file 文件对象 -- 从那里移动
* @param destPath 目标路径 -- 移动到哪里
*/
public static void moveFile(File file, String destPath){
try {
saveAsFile(file, destPath); //拷贝
deleteFile(file);//删除
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//TODO -- 删除
/**
* 删除文件 --传递文件对象
* @param file 文件对象
* @throws FileNotFoundException 文件找不到
*/
public static void deleteFile(File file) throws FileNotFoundException {
if (file.exists()) {// 判断文件是否存在
if (file.isFile()) {// 判断是否是文件
file.delete();// 删除文件
} else if (file.isDirectory()) {// 否则如果它是一个目录
File[] files = file.listFiles();// 声明目录下所有的文件 files[];
for (int i = 0; i < files.length; i++) {// 遍历目录下所有的文件
deleteFile(files[i]);// 把每个文件用这个方法进行迭代
}
file.delete();// 删除文件夹
}
} else {
throw new FileNotFoundException();
}
}
/**
* 删除文件 -- 传递文件路径
* @param filePath 文件路径
* @throws FileNotFoundException 文件找不到
*/
public static void deleteFile(String filePath) {
try {
deleteFile(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//TODO -- 拷贝
/**
* 保存文件 -- 可以是文件,也可以是一个文件夹
* @param filePath 文件路径 --从哪里拷贝
* @param destPath 要保存的路径 -- 拷贝到哪里
*
* 例如:saveAsFile("D:\\fisp_core_2019-02-28.xsd", "d:\\11\\fisp_core_2019-02-28.xsd");
* 生成D:\11\fisp_core_2019-02-28.xsd,D:\\fisp_core_2019-02-28.xsd不会删除,相当于拷贝
* 例如:saveAsFile("d:\\11", "d:\\122");
* 生成D:\122\fisp_core_2019-02-28.xsd和D:\122\fisp_core_2019-02-28.xsd1,11文件夹下有两个文件
*/
public static void saveAsFile(String filePath, String destPath){
File file = new File(filePath);
saveAsFile(file, destPath);
}
/**
* 保存文件 -- 可以是文件,也可以是一个文件夹
* @param file 文件对象 --从哪里拷贝
* @param destPath 要保存的文件路径 -- 拷贝到哪里
*/
public static void saveAsFile(File file, String destPath){
if (file.isDirectory()) {
//文件夹
File[] files = file.listFiles();// 声明目录下所有的文件 files[];
for (File filei : files) {// 遍历目录下所有的文件
saveAsFile(filei,destPath+File.separator+filei.getName());// 把每个文件用这个方法进行迭代
}
} else {
//文件
FileInputStream in = null ;
try {
in = new FileInputStream(file);
saveAsFile(in, destPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 保存文件 --只能处理单个文件,不能是文件夹
* @param in 文件输入流对象
* @param destPath 要保存的路径,含有文件后缀名
*/
public static void saveAsFile(InputStream in, String destPath){
FileOutputStream out = null;
BufferedOutputStream Bout = null;
try {
byte[] buf = new byte[1024];
File file = new File(destPath);
if (!file.exists()) {
(new File(file.getParent())).mkdirs();
}
out = new FileOutputStream(file);
Bout = new BufferedOutputStream(out);
int b;
while ((b = in.read(buf)) != -1) {
Bout.write(buf,0,b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(Bout != null){
try {
Bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//测试方法
public static void main(String[] args) {
moveFile("d:\\122", "d:\\123");
}