您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > Java-基于命令行文件管理器
packagefilemanager;importjava.io.*;importjava.security.*;importjava.util.*;importjavax.crypto.*;publicclassFileManager{publicstaticvoidmain(String[]args)throwsException{MainMenumainmenu=newMainMenu();mainmenu.StartMenu();}}classCreateFile{CreateFile()throwsIOException{Scanners=newScanner(System.in);System.out.println(Wheretocreate?(D:/aaa/...));StringCurrentDirectory=s.next();Filefile=newFile(CurrentDirectory);if(file.exists()){System.out.println(Enterthename:);StringName=s.next();Filef=newFile(CurrentDirectory+File.separator+Name);//实例化File类的对象f.mkdir();System.out.println(successtocreate!);}elseSystem.out.println(thedirectoryisn'texist!);}}classDeleteFile{Filepath;publicDeleteFile(Filepath){this.path=path;}publicvoidDelete(Filepath){Filefilelist[]=path.listFiles();intlistlen=filelist.length;for(inti=0;ilistlen;i++){if(filelist[i].isDirectory()){Delete(filelist[i]);}else{filelist[i].delete();}}path.delete();//删除当前目录}voidSuccess(){System.out.println(Successtodelete!!:);}}classEnterDirectory{EnterDirectory(Stringpath)throwsIOException{Runtime.getRuntime().exec(explorer.exe+path);}}classListDirectory{ListDirectory(Stringpath){//Stringluj=null;//路径//如果dir不以文件分隔符结尾,自动添加文件分隔符if(!path.endsWith(File.separator)){path=path+File.separator;}FiledirFile=newFile(path);//如果dir对应的文件不存在,或者不是一个文件夹则退出if(!dirFile.exists()||(!dirFile.isDirectory())){System.out.println(Listfailed!cannotfindthedirectory:+path);}//list方法返回该目录下的所有文件(包括目录)的文件名,文件名不含路径信息System.out.println(path+containsbelowdirectoriseorfiles);String[]files=dirFile.list();for(inti=0;ifiles.length;i++){System.out.println(files[i]);}}}classCopyFile{CopyFile(Stringsource,Stringdest)throwsIOException{Filein=newFile(source);Fileout=newFile(dest);FileInputStreaminFile=newFileInputStream(in);FileOutputStreamoutFile=newFileOutputStream(out);byte[]buffer=newbyte[1024];inti=0;while((i=inFile.read(buffer))!=-1){outFile.write(buffer,0,i);}//endwhileinFile.close();outFile.close();System.out.print(Successtocopy!);}}classCopyDirectory{publicvoidcopyFolder(StringoldPath,StringnewPath){try{(newFile(newPath)).mkdirs();//如果文件夹不存在则建立新文件夹Filea=newFile(oldPath);String[]file=a.list();Filetemp=null;for(inti=0;ifile.length;i++){if(oldPath.endsWith(File.separator)){temp=newFile(oldPath+file[i]);}else{temp=newFile(oldPath+File.separator+file[i]);}if(temp.isFile()){FileInputStreaminput=newFileInputStream(temp);FileOutputStreamoutput=newFileOutputStream(newPath+/+(temp.getName()).toString());byte[]b=newbyte[1024*5];intlen;while((len=input.read(b))!=-1){output.write(b,0,len);}output.flush();output.close();input.close();}if(temp.isDirectory()){//如果是子文件夹copyFolder(oldPath+/+file[i],newPath+/+file[i]);}}}catch(Exceptione){System.out.println(复制整个文件夹内容操作出错);e.printStackTrace();}}publicvoidSuccess(){System.out.println(Successtocopythedirectory!);}}classTestDES{Keykey;publicTestDES(){}publicTestDES(Stringstr){getKey(str);//生成密匙}/***根据参数生成KEY*/publicvoidgetKey(StringstrKey){try{KeyGenerator_generator=KeyGenerator.getInstance(DES);_generator.init(newSecureRandom(strKey.getBytes()));this.key=_generator.generateKey();_generator=null;}catch(Exceptione){thrownewRuntimeException(ErrorinitializingSqlMapclass.Cause:+e);}}/**文件file进行加密并保存目标文件destFile中**@paramfile要加密的文件如c:/test/srcFile.txt*@paramdestFile加密后存放的文件名如c:/加密后文件.txt*/publicvoidencrypt(Stringfile,StringdestFile)throwsException{//【具体用法见主函数】Ciphercipher=Cipher.getInstance(DES);//cipher.init(Cipher.ENCRYPT_MODE,getKey());cipher.init(Cipher.ENCRYPT_MODE,this.key);InputStreamis=newFileInputStream(file);OutputStreamout=newFileOutputStream(destFile);CipherInputStreamcis=newCipherInputStream(is,cipher);byte[]buffer=newbyte[1024];intr;while((r=cis.read(buffer))0){out.write(buffer,0,r);}cis.close();is.close();out.close();}/**文件采用DES算法解密文件**@paramfile已加密的文件如c:/加密后文件.txt**@paramdestFile*解密后存放的文件名如c:/test/解密后文件.txt*/publicvoiddecrypt(Stringfile,Stringdest)throwsException{Ciphercipher=Cipher.getInstance(DES);cipher.init(Cipher.DECRYPT_MODE,this.key);InputStreamis=newFileInputStream(file);OutputStreamout=newFileOutputStream(dest);CipherOutputStreamcos=newCipherOutputStream(out,cipher);byte[]buffer=newbyte[1024];intr;while((r=is.read(buffer))=0){cos.write(buffer,0,r);}cos.close();out.close();is.close();}voidDES()throwsException{System.out.println(inuputencryptedpassword:);Scannerscan=newScanner(System.in);Stringtd1=scan.nextLine();//以上两行用来实现字符串的输入。TestDEStd=newTestDES(td1);//【对称加密的密码所在】System.out.println(encrypt:inuputsourcefileandtargetfile:);Stringsourcee=scan.next();Stringtargete=scan.next();td.encrypt(sourcee,targete);//加密System.out.println(encryptioniscomplete!);System.out.println(decrypt:inuputdecryptionedpassword:);Scannerscanf=newScanner(System.in);Stringtd2=scanf.nextLine();TestDEStdd=newTestDES(td2);System.out.println(decrypt:inuputsourcefileandtargetfile:);Stringsourced=scan.next();Stringtargetd=scan.next();tdd.decrypt(sourced,targetd);//解密System.out.println(decryptioniscomplete);}}classMainMenu{voidStartMenu()throwsException{System.out.println(Choosewhatt
本文标题:Java-基于命令行文件管理器
链接地址:https://www.777doc.com/doc-7246833 .html