一、
//1、创建文件夹
public static boolean createFolder(String pathName){
try {
File folderPath = new File(pathName);
if(!folderPath.exists()){
folderPath.mkdir();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("新建文件夹时,出错!");
return false;
}
}
** //2、创建文件**
public static boolean createFlie(String filePath){
try {
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println(filePath);
fw.close();
pw.close();
return true;
} catch (IOException e) {
e.printStackTrace();
System.err.println("创建文件时,出错!");
return false;
}
}
** //3、删除空文件夹**
public static boolean deleteFolder(String floderPath){
try {
File delFloder = new File(floderPath);
delFloder.delete();
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("删除文件夹时,出错!");
return false;
}
}
** //4、删除指定文件**
public static boolean deleteFile(String filePath){
try {
File delFlie = new File(filePath);
delFlie.delete();
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("删除文件时,出错!");
return false;
}
}
** //5、删除某文件夹下的所有空文件夹**
public static boolean deleteFloderHasContent(String floderPath){
try {
File delFloderC = new File(floderPath);
File[] files = delFloderC.listFiles();
System.err.println(delFloderC.length());
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory()){
files[i].delete();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("删除有内容的文件夹时,出错!");
return false;
}
}
** //6、记事本写入文件**
public static boolean readFile(String sourcePath, String content){
try {
File file = new File(sourcePath);
FileWriter fw = new FileWriter(file);
fw.write(content);
fw.flush();
fw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("写入文件时,出错!");
return false;
}
}
** //7、读取文件属性**
public static void readFileMsg(String sourcePath){
File file = new File(sourcePath);
if(file.exists()){
System.err.println("文件名称" + file.getName());
System.err.println(file.isFile()?"文件":"不是文件");
System.err.println(file.isDirectory()?"文件夹":"非文件夹");
System.err.println(file.canRead()?"可读取":"不可读取");
System.err.println(file.canWrite()?"是隐藏文件":"不是隐藏文件");
System.err.println("最终修改时间"+new Date(file.lastModified()));
System.err.println(file.canExecute()?"是执行文件":"不是执行文件");
System.err.println("文件大小"+file.getTotalSpace());
}
}
//8、复制文件夹
public static boolean copyFloder(String sourcePath, String totalPath){
File file = new File(sourcePath);
String[] fileLst = file.list();
File tarFile = new File(totalPath);
if(!tarFile.exists()){//创建,目标复制文件夹
tarFile.mkdir();
}
for (int i = 0; i < fileLst.length; i++) {
if((new File(sourcePath + file.separator + fileLst[i])).isDirectory()){//若文件夹中包含文件夹,则递归调用
copyFloder(sourcePath + file.separator + fileLst[i], totalPath + file.separator + fileLst[i]);
}
if((new File(sourcePath + file.separator + fileLst[i])).isFile()){//文件夹内的文件
System.err.println(sourcePath + file.separator + fileLst[i]);
copyFile(sourcePath + file.separator + fileLst[i], totalPath + file.separator + fileLst[i]);
}
}
return false;
}
//9、复制文件
public static boolean copyFile(String sourcePath, String targetPath){
try {
File file = new File(sourcePath);
int byteread = 0;
if(file.exists()){
FileInputStream fi = new FileInputStream(file);//读取源文件
FileOutputStream fo = new FileOutputStream(new File(targetPath));//写入目标文件
byte[] buffer = new byte[1024];
while ((byteread = fi.read(buffer)) != -1) {
fo.write(buffer, 0, byteread);
}
fi.close();
fo.close();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("文件复制时,出错!");
return false;
}
}
二、测试文件夹的复制
我喜欢音乐,因此就拿这个文件夹做测试用例吧
String floderPath = "D:\\red_ant_file\\20181106\\KGMusic";
String totalPath = "D:\\red_ant_file\\20181106\\KGMusicCopy";
AllServiceIsHere.copyFloder(floderPath, totalPath);
运行后
完工!