做一个控制台版的文件管理器,要求:
2.1 创建文件/文件夹
2.2 删除文件/文件夹
2.3 复制文件
2.4 移动文件
2.5 重命名文件/文件夹
Files类
package com.gxa.day21.homework.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
/**
* 用来处理文件/文件夹的增删改
* @author:WWA
* @date:Created at 2020/12/28
*/
public class FileUtil {
/**
* 基本的路径
*/
private String basePath;
public FileUtil(){
basePath = "D:\\worksapce_for_idea\\java246\\src\\com\\gxa\\day21\\homework";
}
public FileUtil(String basePath){
this.basePath = basePath;
}
/**
* 创建文件
* @param path : 文件夹路径
* @param fileName : 文件名
* @return
*/
public boolean createFile(String path, String fileName){
try {
if (path == null || path.isEmpty() || fileName == null || fileName.isEmpty()) {
return false;
}
// 将路径构建成File对象
File dir = new File(path);
if (!dir.exists()){
dir.mkdirs();
}
// 判断是否是文件夹
if (!dir.isDirectory()) {
return false;
}
// 构建文件的File对象
File file = new File(dir, fileName);
if (file.exists()) {
return false;
}
return file.createNewFile();
} catch (IOException e) {
// e.printStackTrace();
return false;
}
}
/**
* 创建文件夹的方法
* @param path : 路径
* @param dirName : 文件夹名字
* @return
*/
public boolean createDir(String path, String dirName){
if (path == null || path.isEmpty() || dirName == null || dirName.isEmpty()) {
return false;
}
// 将路径构建成File对象
File dir = new File(path);
if (!dir.exists()){
dir.mkdirs();
}
// 判断是否是文件夹
if (!dir.isDirectory()) {
return false;
}
// 构建文件夹的File对象
File file = new File(dir, dirName);
if (file.exists()) {
return false;
}
return file.mkdirs();
}
/**
* 删除文件/文件夹
* @param path : 路径
* @param fileName : 文件名/文件夹名字
* @return
*/
public boolean deleteFile(String path, String fileName){
if (path == null || path.isEmpty() || fileName == null || fileName.isEmpty()) {
return false;
}
// 构建path的文件对象
File dir = new File(path);
if (!dir.exists()) {
return false;
}
// 判断path是否是文件夹
if (!dir.isDirectory()) {
return false;
}
// 构建需要删除的文件对象
File file = new File(dir, fileName);
// 判断是否存在
if (!file.exists()) {
return false;
}
// 判断是否是文件
if (file.isFile()) {
// 是文件就直接删除
file.delete();
}
// 判断是否是文件夹
if (file.isDirectory()) {
// 获取下面的所有的文件列表
String[] list = file.list();
// 遍历
for (String fileStr : list) {
// 直接递归
deleteFile(file.getPath(), fileStr);
}
// 删除外层的文件夹
file.delete();
}
return true;
}
/**
* 复制单个文件
* @param source : 源文件的路径
* @param target : 目标文件的路径
* @return
*/
public boolean copyFile(String source, String target){
try {
// static Path copy(Path source, Path target, CopyOption... options)
// 构建源文件的File对象
File srcFile = new File(source);
if (!srcFile.exists()) {
return false;
}
// 构建目标文件的file对象
File targetFile = new File(target);
if (targetFile.exists()) {
return false;
}
Files.copy(srcFile.toPath(), targetFile.toPath());
return true;
} catch (IOException e) {
// e.printStackTrace();
return false;
}
}
/**
* 移动文件
* @param source
* @param target
* @return
*/
public boolean moveFile(String source, String target){
try {
// static Path copy(Path source, Path target, CopyOption... options)
// 构建源文件的File对象
File srcFile = new File(source);
if (!srcFile.exists()) {
return false;
}
// 构建目标文件的file对象
File targetFile = new File(target);
if (targetFile.exists()) {
return false;
}
Files.move(srcFile.toPath(), targetFile.toPath());
return true;
} catch (IOException e) {
// e.printStackTrace();
return false;
}
}
}