java文件管理器.doc
《java文件管理器.doc》由会员分享,可在线阅读,更多相关《java文件管理器.doc(16页珍藏版)》请在冰点文库上搜索。
![java文件管理器.doc](https://file1.bingdoc.com/fileroot1/2023-4/28/bcf574e1-0464-457d-816a-f791d61b3b4a/bcf574e1-0464-457d-816a-f791d61b3b4a1.gif)
《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)在实验