黑马程序员技术交流社区
标题:
复制、删除、剪切、文件夹
[打印本页]
作者:
354620815
时间:
2014-10-19 22:35
标题:
复制、删除、剪切、文件夹
package com.itheima;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制、删除、剪切、文件夹
*
* File对象可以是一个已存在的文件,也可以是一个不存在的文件.
* new 一个 File 对象只是创建一个文件对象, 不等于创建一个文件
*
*/
public class FFFF {
public static void main(String[] args) throws IOException {
File dir = new File("123");
File dir2 = new File("C:\\000");
// copy(dir,dir2);
// delectDir(dir);
cut(dir2,dir);
}
/**
* 删除指定文件夹...:从里往外删...
*
* 思路:
* 接收一个文件对象,判断文件是否存在
* 如果是文件直接删除
* 如果是文件夹就获取文件夹里面的文件列表
* 遍历文件列表,如果碰到文件夹就开始递归了
* 先删除里面的文件,再删除文件夹
*
* @param dir 要删除的文件夹
* @return 是否删除成功
*/
private static boolean delectDir(File dir) {
if (!dir.exists()) throw new RuntimeException("文件不存在");
if (dir.isFile()) return dir.delete();
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) delectDir(file);
file.delete();
}
return dir.delete();
}
/**
* 复制文件夹中的所有文件,到指定的文件夹中.
*
* 从外往里复制
*
* 思路:
* 接收两个文件对象,一个是源文件夹,一个是目的地的。
* 获取源文件夹的所有文件列表
* 遍历文件列表,如果是文件夹,就在目的地对应的目录下创建一个同名的文件夹...
* 如果是文件的话,就要使用IO流复制.
*
* 复制文件夹使用复制文件夹的方法,文件夹里面可能还有文件夹还是使用复制文件夹的方法
* 因为功能重复,所有可以使用递归.
*
* @param oldPath 要复制的文件夹
* @param newPath 复制到哪个文件夹
* @throws IOException
*/
private static void copy(File oldPath, File newPath) throws IOException {
if (!oldPath.exists())
throw new RuntimeException("文件不存在");
// 如果重名
if (oldPath.getAbsolutePath().equals(newPath.getAbsolutePath()))
newPath = new File(newPath.getAbsolutePath() + " - 副本");
if (!newPath.exists()) newPath.mkdir();
if (oldPath.isFile()) {
copyFile(oldPath,new File(newPath, oldPath.getName())); // ↓↓↓↓↓↓
return;
}
copyFunc(oldPath, newPath); // ↓↓↓↓↓↓
}
/**
* 复制文件夹的递归方法
* @param oldPath 要复制的文件夹
* @param newPath 复制到哪个文件夹
* @throws IOException
*/
private static void copyFunc(File oldPath, File newPath) throws IOException {
File[] files = oldPath.listFiles();
for (File file : files) {
if (file.isDirectory()) {
File newDir = new File(newPath, file.getName());
newDir.mkdirs();
copyFunc(file, newDir);
} else {
copyFile(file,new File(newPath, file.getName())); // ↓↓↓↓↓↓
}
}
}
/**
* IO复制文件的方法
* @param from 源文件
* @param to 目的地
* @throws IOException
*/
private static void copyFile(File from, File to) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(from);
fos = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (fos != null)
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 剪切文件夹
* 先复制,后删除
* 其实应该复制一个删一个
* @param dir
* @param dir2
* @throws IOException
*/
private static void cut(File oldPath, File newPath) throws IOException {
copy(oldPath, newPath);
delectDir(oldPath);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2