📄 filedeal.java
字号:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/*
* Created on 2005-12-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class FileDeal {
public static void main(String[] args) {
//FileDeal f=new FileDeal();
//File file = new File("C:\\test\\J2EE");
//System.out.println(file.getAbsolutePath()+" "+file.getPath());
//FileDeal.delNotEmptyDir(file);
//copyNotEmptyDir("C:\\test\\J2EE","C:\\TEMP");
//moveDir("C:\\test\\J2EE","C:\\TEMP");
}
/**
* delete not empty directory
*
* @param file
* @return -1 denote fail,other:success
*/
public static void delNotEmptyDir(File dir) {
// 删除目录下的所有文件
delAllfile(dir);
// 删除自己
dir.delete();
}
/**
* delete all file in one directory
*
* @param file
* @return
*/
public static void delAllfile(File dir) {
if (!dir.exists()) {
return;
} else {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
} else if (files[i].isDirectory()) {
delAllfile(files[i]);
files[i].delete();
}
}
////// test
/*
* for(int i=0;i <files.length;i++){
* System.out.println(files[i].getAbsolutePath()); }
* System.out.println(File.separator);
*/
////////
}
}
/**
*
* @param dir
* @param targetDir
*/
public static void copyNotEmptyDir(String oldPaht,String newPath) {
File dir=new File(oldPaht);
File targetDir=new File(newPath);
copyNotEmptyDir(dir,targetDir);
}
/**
* precondition:the first directory is exist and not empty, copy not empty
* directory to target directory
*
* @param dirName
*/
public static void copyNotEmptyDir(File dir, File targetDir) {
if (!targetDir.exists()){
System.out.println("指定的目标目录不存在");
return ;
}else{
if (!targetDir.isDirectory()){
System.out.println("指定的不是目录");
return ;
}else{
String subFileName=dir.getName();
File subDir=new File(targetDir.getPath()+File.separator+subFileName);
System.out.println("生成 "+targetDir.getPath()+" 子目录 "+subDir.getName());
subDir.mkdir();
copyAllFile(dir,subDir);
}
}
}
/**
* copy 当前目录下的文件和空目录到指定的目录
* @param oldPath
* @param newPath
*/
public static void copyAllFile(String oldPath, String newPath) {
File dir=new File(oldPath);
File targetDir=new File(newPath);
copyAllFile(dir,targetDir);
}
/**
* copy 当前目录下的文件和空目录到指定的目录
*
* @param dir
* @param targetDir
*/
public static void copyAllFile(File dir, File targetDir) {
String subFileName="";
File newFile=null;
if (!dir.exists()) {
return;
} else {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
subFileName=files[i].getName();
newFile=new File(targetDir.getPath()+File.separator+subFileName);
if (files[i].isFile()) {
copyFile(files[i],newFile);
}else if (files[i].isDirectory()){
System.out.println("the old directory :"+files[i].getPath());
System.out.println("make new directory :"+newFile.getPath());
newFile.mkdir();
copyAllFile(files[i],newFile);
}
}
}
}
/**
* copy single file
* @param oldPath
* @param newPath
*/
public static void copyFile(String oldPath,String newPath){
File oldFile=new File(oldPath);
File newFile=new File(newPath);
copyFile(oldFile,newFile);
}
/**
* copy single file
* @param oldFile
* @param newFile
*/
public static void copyFile(File oldFile, File newFile) {
System.out.println("拷贝文件");
System.out.println("原文件 "+oldFile.getPath());
System.out.println("新文件 "+newFile.getPath());
//int bytesum = 0;
//int byteread = 0;
if (oldFile.exists()) { //文件存在时
try {
InputStream inStream = new FileInputStream(oldFile); //读入原文件
FileOutputStream fs = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
while (inStream.read(buffer)!= -1) {
//bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer);
}
inStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*precondition: the oldFile is exist
* @param oldFile
* @param newFile
*/
public static void moveFile(File oldFile,File newFile){
copyFile(oldFile,newFile);
oldFile.delete();
}
/**
*precondition :the oldPath of the Directory is exist;
* @param oldPath
* @param newPath
*/
public static void moveDir(String oldPath,String newPath){
File oldDir=new File(oldPath);
File targetDir=new File(newPath);
moveDir(oldDir,targetDir);
}
/**
* precondition: the oldDir is exist
* @param oldDir
* @param newDir
*/
public static void moveDir(File oldDir,File targetDir){
copyNotEmptyDir(oldDir,targetDir);
delNotEmptyDir(oldDir);
}
/**
* 前提:该文件是目录且存在 //-1代表该目录为空目录,其它代表非空目录
*
* @param file
* @return
*/
public static int dirIsEmpty(File file) {
String[] fileNames = file.list();
if (fileNames == null) {
return -1;
} else {
return 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -