您好,欢迎访问三七文档
报告创建时间:《Java程序设计》实验报告年级、专业、班级2011级计科3班2011级计科3班姓名江唯伟张桉实验题目命令行文件管理器实验时间2013.10.14实验地点主教0414实验成绩实验性质□验证性□设计性■综合性教师评价:□算法/实验过程正确;□源程序/实验内容提交□程序结构/实验步骤合理;□实验结果正确;□语法、语义正确;□报告规范;其他:评价教师签名:一、实验目的运用面向对象程序设计思想,基于Java文件管理和I/O框架,实现命令行下的文件管理器。二、实验项目内容1、实现文件夹创建、删除、进入。2、实现当前文件夹下的内容罗列,能够根据文件名、日期、大小,类型进行排序。3、实现文件拷贝和文件夹拷贝(文件夹拷贝指深度拷贝,包括所有子目录和文件)。4、实现指定文件的加密和解密。5、实现多个文件的压缩和解压缩。6、实现一个大文件的分割和合并。7、实现把项目导出为可执行的*.jar文件。三、实验过程或算法(源程序)要求:写明设计思想、程序的结构、功能关系图、类的说明并按照UML的规范画出类之间的关系图、程序执行流程图,最后是源代码。设计思想:主界面给出所有的选项,通过switch-case事件,来选择不同功能。运用到try-catch处理异常。文件的操作用到了:FileInputStream和FileOutputStream,BufferedInputStream和BufferedOutputStream,Scanner等流。程序的结构:src里共含有四个java文件,分别为压缩、解压缩、分割与合并、main和其余功能选项。功能关系图:创建目录删除目录复制文件复制目录加密解密文件主界面列出目录重命名文件的压缩与解压文件的分割与合成退出程序执行流程图:开始↓进入主界面↓按下键盘上相应数字按钮↓↓按下数字1~9按下数字0↓↓根据提示输入数据退出实现功能是↓否是否继续?源代码:创建目录:classCreateFile{CreateFile(){try{System.out.println(wheretocreate?+pleaseinputthepath:);Scanners=newScanner(System.in);Stringpath=s.next();Filefile=newFile(path);if(file.exists()){System.out.println(pleaseinputthefilename!+例如F:/src/sb。txt);Stringname=s.next();Filef=newFile(path+File.separator+name);f.mkdir();System.out.println(successtocreate!);}elseSystem.out.println(thedirectoryisn'texist!);}catch(Exceptione){e.printStackTrace();}}}删除目录:classDeleteFile{Filepath;publicDeleteFile(Filepath){this.path=path;}publicvoidDelete(Filepath){File[]files=path.listFiles();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){Delete(files[i]);}else{files[i].delete();}}path.delete();}}复制文件:classCopyFile{CopyFile(Stringsource,Stringtarget){try{Filef1=newFile(source);Filef2=newFile(target);FileInputStreaminputFile=newFileInputStream(f1);FileOutputStreamoutputFile=newFileOutputStream(f2);byte[]b=newbyte[1024*10];intvalue=0;while((value=inputFile.read(b))!=-1){outputFile.write(b,0,value);}outputFile.flush();outputFile.close();inputFile.close();}catch(Exceptione){e.printStackTrace();}}}复制目录:classCopyDirectory{publicvoidcopyFolder(StringoldPath,StringnewPath){try{(newFile(newPath)).mkdirs();Fileo=newFile(oldPath);String[]file=o.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()){FileInputStreaminputDir=newFileInputStream(temp);FileOutputStreamoutputDir=newFileOutputStream(newPath+/+(temp.getName()).toString());byte[]b=newbyte[1024*10];intvalue;while((value=inputDir.read(b))!=-1){outputDir.write(b,0,value);}outputDir.flush();outputDir.close();inputDir.close();}if(temp.isDirectory()){copyFolder(oldPath+/+file[i],newPath+/+file[i]);}}}catch(Exceptione){e.printStackTrace();}}}列出目录:classListDirectory{ListDirectory(Stringpath){Filedf=newFile(path);if(!df.exists()||(!df.isDirectory())){System.out.println(Listfailed!);}System.out.println(path+/+containsbelowdirectoriseorfiles);String[]files=df.list();for(inti=0;ifiles.length;i++){System.out.println(files[i]);}}}进入目录:classEnterDirectory{EnterDirectory(Stringpath){Filedf=newFile(path);if(df.exists()){System.out.println(已进入此目录!);}else{System.out.println(此目录不存在!);}}}重命名:Filenamepath=newFile(s.next());if(namepath.exists()){System.out.println(inputnewname);Filenewname=newFile(s.next());if(namepath.renameTo(newname)==true){System.out.println(Successtorename!);}else{System.out.println(fileexist);}}加密解密文件: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);}}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();}publicvoiddecrypt(Stringfile,Stringdest)throwsException{Ciphercipher=Cipher.getInstance(DES);cipher.init(Cipher.DECRYPT_MODE,this.key);InputStreamis=newFileInputStream(file);OutputStreamout=newFileOutputStream(dest);CipherOutputStreamcou=newCipherOutputStream(out,cipher);byte[]buffer=newbyte[1024];intr;while((r=is.read(buffer))=0){cou.write(buffer,0,r);}cou.close();out.close();is.close();}压缩与解压缩:publicclassTestZIP{publicstaticvoidzipFiles(File[]srcfile,Filezipfile){try{//ZipOutputStream类:完成文件或文件夹的压缩ZipOutputStreamout=newZipOutputStream(newFileOutputStream(zipfile));for(inti=0;isrcfile.length;i++){FileInputStreamin=newFileInputStream(srcfile[i]);out.putNextEntry(newZipEntry(srcfile[i].getName()));byte[]buf=newbyte[1024];intlen;while((len=in.read(buf))0){out.write(buf,0,len);}out.closeEntry();in.close();}out.close();System.out.println(压缩完成.);}catch(Exceptione){e.printStackTrace();}}publicstaticvoidunZipFiles(Filezipfile,StringdescDir){try{ZipFilezf=newZipFile(z
本文标题:java文件管理器
链接地址:https://www.777doc.com/doc-4484174 .html