java文件资料管理系统器.docx
- 文档编号:15052526
- 上传时间:2023-06-30
- 格式:DOCX
- 页数:21
- 大小:208.95KB
java文件资料管理系统器.docx
《java文件资料管理系统器.docx》由会员分享,可在线阅读,更多相关《java文件资料管理系统器.docx(21页珍藏版)》请在冰点文库上搜索。
java文件资料管理系统器
《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!
");
}else
System.out.println("thedirectoryisn'texist!
");
}catch(Exceptione){
e.printStackTrace();
}
}
}
删除目录:
classDeleteFile{
Filepath;
publicDeleteFile(Filepath){
this.path=path;
}
publicvoidDelete(Filepath){
File[]files=path.listFiles();
for(inti=0;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;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;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;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(zipfile); for(Enumerationentries=zf.entries();entries.hasMoreElements();){ ZipEntryentry=(ZipEntry)entries.nextElement(); StringzipEntryName=entry.getName(); InputStreamin=zf.getInputStream(entry); OutputStreamout=newFileOutputStream(descDir+zipEntryName); byte[]buf1=newbyte[1024]; intlen; while((len=in.read(buf1))>0){ out.write(buf1,0,len); } in.close(); out.close(); System.out.println(entry); } }catch(Exceptione){ e.printStackTrace(); } } } 分割与合并: publicclassFileDivisionUniter{ publicstaticfinalStringSUFFIX=".txt"; publicstaticString[]divide(StringfileName,longsize)throwsException{ FileinFile=newFile(fileName); if(! inFile.exists()||inFile.isDirectory()){ thrownewException("指定文件不存在! "); } FileparentFile=inFile.getParentFile();//获得被分割文件父文件,将来被分割成的小文件就存放在这个目录下 longfileLength=inFile.length();//取得文件的大小 if(size<=0) size=fileLength/2; intnum=(int)((fileLength+size-1)/size);//取得分割后的小文件的数目 String[]outFileNames=newString[num];//存放分割后的小文件名 BufferedInputStreamfis=newBufferedInputStream(newFileInputStream( inFile),10*1024*1024);//用10M的缓冲读取 BufferedReaderreader=newBufferedReader(newInputStreamReader(fis)); Strings=reader.readLine(); //读输入文件流的开始和结束下标 longinEndIndex=0; intinBeginIndex=0; //根据要分割的数目分割文件 for(intoutFileIndex=0;outFileIndex //对于前outFileIndex-1个文件,大小都是size FileoutFile=newFile(parentFile,inFile.getName()+outFileIndex +SUFFIX); FileOutputStreamout=newFileOutputStream(outFile); inEndIndex+=size; inEndIndex=(inEndIndex>fileLength)? fileLength: inEndIndex; //从输入流中读取字节存储到输出流中 for(;inBeginIndex out.write(reader.read()); out.close(); outFileNames[outFileIndex]=outFile.getAbsolutePath(); } fis.close(); returnoutFileNames; } publicstaticStringunite(String[]fileNames,StringtargerFileName) throwsException{ FileinFile=null; FileoutFile=newFile(targerFileName); FileOutputStreamout=newFileOutputStream(outFile); for(inti=0;i inFile=newFile(fileNames[i]); FileInputStreamin=newFileInputStream(inFile); intc; while((c=in.read())! =-1) out.write(c); in.close(); } out.close(); returnoutFile.getAbsolutePath(); } } 四、实验结果及分析和(或)源程序调试过程(包含程序运行截图) 主界面: 1.创建目录: 2.删除目录: 3.复制文件: 4.复制目录: 5.加密解密文件: 6.进入目录: 7.列出目录: 8.重命名: 9.对文件夹进行压缩与解压缩: 10.对文件进行分割与合成: 0.退出程序: 程序运行成功。 备注: 1、教师在布置需撰写实验报告的实验前,应先将报告书上的“实验题目”、“实验性质”、“实验目的”、“实验项目内容”等项目填写完成,然后再下发给学生。 2、教师在布置需撰写报告的实验项目时,应告知学生提交实验报告的最后期限。 3、学生应按照要求正确地撰写实验报告: 1)在实验报告上正确地填写“实验时间”、“实验地点”等栏目。 2)将实验所涉及的源程序文件内容(实验操作步骤或者算法)填写在“实验过程或算法(源程序)”栏目中。 3)将实验所涉及源程序调试过程(输入数据和输出结果)或者实验的分析内容填写在“实验结果及分析和(或)源程序调试过程”栏目中。 4)在实验报告页脚的“报告创建时间: ”处插入完成实验报告时的日期和时间。 5)学生将每个实验完成后,按实验要求的文件名通过网络提交(上载)到指定的服务器所规定的共享文件夹中。 每个实验一个电子文档,如果实验中有多个电子文档(如源程序或图形等),则用WinRAR压缩成一个压缩包文档提交,压缩包文件名同实验报告文件名(见下条)。 6)提交的实验报告电子文档命名为: 实验序号(1位数字)_年级(四位数字不要“级”字)专业(缩写: 计算机科学与技术专业(计科)、网络工程专业(网络)、信息安全专业(信息)、物联网工程(物联网))项目组成员(学号(八位数字)姓名).doc。 如完成第1个Project,专业为“计算机科学与技术”,“网络工程专业”专业,项目组成员有: 张三(学号20115676),李四(学号20115
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 文件 资料 管理 系统