文件管理系统
整理:Ackarlix
运用java.io包下的API进行windows下的文件管理,包括文件及文件夹删除、文件及文件夹属性察看、文件及文件夹拷贝、文件及文件夹创建。
小地快做完了,可是在copyFile的时候总是出错,请大家看看小第的代码,多提意见,谢啦
qq:305821203 mail:xiaohai_34@163.com
import java.io.*;
import java.util.ArrayList;
class FileSystem{
public static void main(String[] args) throws IOException{
System.out.println("************1.Look File Data ***********");
System.out.println("************2.Create File ***********");
System.out.println("************3.Delete File ***********");
System.out.println("************4.Copy file ***********");
System.out.print("Please give me number:");
BufferedReader miannum=new BufferedReader(new InputStreamReader(System.in));
switch(miannum.readLine().charAt(0)){
case '1':
new LookFile();break;
case '2':
new CreateFile();break;
case '3':
new DelFile();break;
case '4':
new CopyFile();break;
default:System.out.println("Err Menu Number");
}
}
}
class CreateFile{
private String filename;
private String filepath;
CreateFile() throws IOException{
System.out.print("Please give me a File Path:");
BufferedReader NFpath=new BufferedReader(new InputStreamReader(System.in));
filepath=NFpath.readLine();
System.out.print("Please give me a new File Name:");
BufferedReader NFname=new BufferedReader(new InputStreamReader(System.in));
filename=NFname.readLine();
if(!CheckFileExist(filepath,filename))
System.out.println("See You!!");
else
Create(filepath,filename,CheckFile(filename));
}
private void Create(String filepath,String filename,String type) throws IOException{
boolean b=false;
File f=new File(filepath+filename);
if (type.equals("file")){
b=f.createNewFile();
}
else if(type.equals("folder")){
b=f.mkdir();
}
if(b)
System.out.println("Create file successful!");
else
System.out.println("Create file Error!");
}
private boolean CheckFileExist(String name,String path){
File f=new File(name+path);
if (f.exists()){
System.out.println("File Name already exist,please delete first");
return false;
}
else{
return true;
}
}
private String CheckFile(String filename){
if (filename.indexOf(".")==-1){
return "folder";
}else{
return "file";
}
}
}
class LookFile{
private String filepath;
LookFile() throws IOException{
String type;
System.out.print("Please give me a File Path or File Name:");
BufferedReader NFname=new BufferedReader(new InputStreamReader(System.in));
filepath=NFname.readLine();
if ((type=CheckFile(filepath)).equals("false")){
System.out.println("FilePath or FileName is not exists");
}else
Look(filepath,type);
}
private void Look(String filepath,String type){
File[] f={null};
if (type.equals("file")){
f[0]=new File(filepath);
}else if(type.equals("folder")){
f=new File(filepath).listFiles();
}
PrinFileData(f);
}
private void PrinFileData(File[] f){
String type="";
System.out.println("File Name/t/t/t"+"Type/t");
for(int i=0;i<f.length;i++){
if(f[i].isFile()){
type="This is File";
}else if(f[i].isDirectory()){
type="This is Folder";
}
System.out.println(f[i].getName()+"/t/t/t"+type+"/t");
}
}
private String CheckFile(String filename){
File f=new File(filename);
if(!(f).exists()){
return "false";
}else if(filename.indexOf(".")==-1)
return "folder";
else
return "file";
}
}
class DelFile{
private String type;
private String filename;
DelFile() throws IOException{
System.out.print("Please give me a FileName or FolderName:");
BufferedReader NFname=new BufferedReader(new InputStreamReader(System.in));
filename=NFname.readLine();
if ((type=CheckFile(filename)).equals("false")){
System.out.println("FilePath or FileName is not exists");
}else
Delete(filename,type);
}
ArrayList allfilename=new ArrayList();
private void Delete(String filename,String type)throws IOException{
File FN=new File(filename);
if(FN.isFile()){
allfilename.add(FN);
return;
}
if(FN.isDirectory()){
File[] tem=FN.listFiles();
GetFolderFileName(tem);
allfilename.add(new File(filename));
}
Object[] allfile=allfilename.toArray();
for(int i=0;i<allfile.length;i++){
((File)allfile[i]).delete();
}
}
private void GetFolderFileName(File[] foldername){
for(int i=0;i<foldername.length;i++){
if(foldername[i].isDirectory()){
this.GetFolderFileName(foldername[i].listFiles());
}
allfilename.add(foldername[i]);
}
}
private String CheckFile(String filename){
File f=new File(filename);
if(!f.exists()){
return "false";
}else if((f.isDirectory()==true)&&((f.listFiles()).length>0)){
return "folder and have file";
}else if((f.isDirectory()==true)&&((f.listFiles()).length==0)){
return "folder and no file";
}else{
return "file";
}
}
}
class CopyFile{
private String basefile;
private String newfilepath;
private String newfilename;
private String type;
CopyFile()throws IOException{
System.out.print("Please give me a Base File name and path:");
BufferedReader basepath=new BufferedReader(new InputStreamReader(System.in));
basefile=basepath.readLine();
System.out.print("Please give me a new File Parent path:");
BufferedReader topath=new BufferedReader(new InputStreamReader(System.in));
newfilepath=topath.readLine();
System.out.print("Please give me a new File name:");
BufferedReader toname=new BufferedReader(new InputStreamReader(System.in));
newfilename=toname.readLine();
type=CheckFile(basefile,newfilepath,newfilename);
if(type.equals("false")){
System.out.println("System Exit Bye-Bye");
}else{
Copy(basefile,newfilepath,newfilename,type);
}
}
private void Copy(String bf,String nfp,String nfn,String type)throws IOException{
if(type.equals("file")){
CopytoFile(bf,nfp,nfn);
}
if(type.equals("folder")){
CopytoFolder(bf,nfp,nfn);
}
}
private void CopytoFolder(String bf,String nfp,String nfn)throws IOException{
File basefilefolder=new File(bf);
File newfilefolder=new File(nfp);
CreateDirectory(basefilefolder,newfilefolder,nfn);
CreateAndCopyFile(basefilefolder,new File(nfp+"//"+nfn));
}
File[] baseallfile=null;
File[] newallfile=null;
private void CreateAndCopyFile(File bf,File nfp)throws IOException{
this.baseallfile=bf.listFiles();
this.newallfile=nfp.listFiles();
for(int i=0;i<baseallfile.length;i++){
if(baseallfile[i].isDirectory()){
CreateAndCopyFile(baseallfile[i],newallfile[i]);
}else if(baseallfile[i].isFile()){
CopytoFile(baseallfile[i].toString(),newallfile[i].getParent(),newallfile[i].getName());
}
}
}
private void CreateDirectory(File bf,File nfp,String nfn){
File[] alldirectory=bf.listFiles();
File newfilefolder2=new File(nfp+"//"+nfn);
newfilefolder2.mkdir();
for(int i=0;i<alldirectory.length;i++){
if(alldirectory[i].isDirectory()){
CreateDirectory(alldirectory[i],newfilefolder2,alldirectory[i].getName());
}
}
}
private void CopytoFile(String bf,String nfp,String nfn)throws IOException{
String tem;
String str="";
BufferedReader in=new BufferedReader(new FileReader(bf));
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(nfp+"//"+nfn)));
while((tem=in.readLine())!=null){
str+=tem+"/n";
}
in=new BufferedReader(new StringReader(str));
while((tem=in.readLine())!=null){
out.println(tem);
}
in.close();
out.close();
}
private String CheckFile(String bf,String nfp,String nfn){
File basefile=new File(bf);
File newfilepath=new File(nfp);
File newfilename=new File(nfn);
if((!basefile.exists())||(!newfilepath.exists())){
System.out.println("BaseFile or ToFile is not exists!");
return "false";
}else if(!newfilepath.isDirectory()){
System.out.println("New Parent File is not exists!");
return "false";
}else if(basefile.isFile()){
return "file";
}else if(basefile.isDirectory()){
return "folder";
}else{
System.out.println("Unknown Wrong!");
return "false";
}
}
}