文件管理系统实现.docx
《文件管理系统实现.docx》由会员分享,可在线阅读,更多相关《文件管理系统实现.docx(28页珍藏版)》请在冰点文库上搜索。
文件管理系统实现
第4部分、文件管理系统实现:
●基本要求:
利用磁盘文件实现操作系统的文件管理功能,主要包括目录结构的管理、外存空间的分配与释放以及空闲空间管理三部分。
●参考学时:
16学时
●实验提示:
1、通过初始化操作建立一个模拟外存空间的文件,在该文件中保存目录和文件内容。
创建该文件时应创建初始的根目录内容、索引节点以及空闲空间位示图。
根目录实为一特殊文件,其内容为“.”和“..”目录项。
2、索引节点应包括类型(目录or文件)、创建日期、大小、磁盘地址(为了简单起见,可采用单级索引方式)
3、显示命令提示符“$”,并根据输入命令完成相应的文件操作:
⏹MD(创建子目录):
创建目录文件,并在父目录文件中增加目录项,最后修改父目录文件大小
⏹CD(切换工作目录):
根据当前目录切换到指定目录;
⏹RD(删除子目录):
搜索所要删除的目录是否为空目录,若是则删除;
⏹MK(创建空文件):
创建指定大小的文件(如输入命令“mktest2000”,表示创建大小为2000字节的test文件),并在父目录中添加文件名称;还应对空闲空间位示图进行适当修改;
⏹DEL(删除文件):
如果所要删除的文件存在,则删除,同时修改父目录内容;还应对空闲空间位示图进行适当修改;
⏹DIR:
列出当前目录的所有目录项。
//packageosDemo;
importjava.io.Serializable;
importjava.util.Calendar;
publicclassFCBimplementsSerializable{
privateStringname;
inttype;
intsize;
Stringcal;
intfirstblock;
publicFCB(Stringname){
if(name.getBytes().length>=6){
this.name=name.substring(0,6);
}
else{
while(name.getBytes().length<6){
name+='\u0000';
}
this.name=name;
}
//this.name=name;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetType(){
returntype;
}
publicvoidsetType(inttype){
this.type=type;
}
publicintgetSize(){
returnsize;
}
publicvoidsetSize(intsize){
this.size=size;
}
publicStringgetCal(){
returncal;
}
publicvoidsetCal(Stringcal){
this.cal=cal;
}
publicintgetFirstblock(){
returnfirstblock;
}
publicvoidsetFirstblock(intfirstblock){
this.firstblock=firstblock;
}
}
//packageosDemo;
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.ObjectOutputStream;
importjava.io.RandomAccessFile;
importjava.util.Calendar;
classOS{
staticBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
staticStringcurrent_directory="";
finalstaticintEMPTY_BLOCK=0xFFFE;
finalstaticintLAST_BLOCK=0xFFFF;
finalstaticintBLOCK_SIZE=1024;
staticintfirstblock=0;
staticintblockcount;
staticStringfilename="";
staticRandomAccessFileraf1;
staticObjectOutputStreamoos;
staticFileOutputStreamfos;
staticDataOutputStreamdos;
staticFilefile;
staticintf=0;//fat表中的块号
staticintblock=0;//写块号与mdmkFCB写入块中
staticintblockno=0;//读块号
staticinttimes=0;
publicstaticvoidmain(String[]args)throwsException{
while(true){
Stringcmd="";
System.out.print(current_directory);
System.out.print("$:
");
try{
cmd=br.readLine();
if((cmd.indexOf("exit"))!
=-1){
break;
}
elseif(cmd.indexOf("dir")!
=-1){
dir(cmd);
}
elseif(cmd.indexOf("md")!
=-1){
md(cmd);
}
elseif(cmd.indexOf("cd")!
=-1){
cd(cmd);
}
elseif(cmd.indexOf("rd")!
=-1){
rd(cmd);
}
elseif(cmd.indexOf("mk")!
=-1){
mk(cmd);
}
elseif(cmd.indexOf("del")!
=-1){
del(cmd);
}
elseif(cmd.indexOf("format")!
=-1){
format(cmd);
}
elseif(cmd.indexOf("fat")!
=-1){
fat();
}
elseif(cmd.indexOf("info")!
=-1){
Stringstr=cmd.substring(5);
inti=Integer.parseInt(str);
info(i);
}
elseif(cmd.indexOf("help")!
=-1){
help();
}
elseif(cmd.indexOf("exit")!
=-1){
exit();
}
else{
System.out.println("errorcommand!
");
}
}catch(Exceptione){
System.out.println(e);
}
}
}
publicstaticvoiddir(Stringcmd)throwsException{
inti=current_directory.length();
Stringstr="";
if(i>0){
str=current_directory.substring(0,i-1);
StringBuffersb=newStringBuffer(str);
intj=sb.lastIndexOf("/");
Strings1=str.substring(j+1);
}
//System.out.println("blockno========"+blockno);
raf1=newRandomAccessFile(file,"r");
raf1.seek(blockno*1024+blockcount*2);
//System.out.println("-------------"+(blockno*1024+16));
byte[]b=newbyte[6];
byte[]b1=newbyte[14];
intByteCount=0;
while(true){
raf1.read(b);
Strings2=newString(b);
if(ByteCount!
=1024){
//System.out.println(judge(b));
//System.out.println(s2);
if(!
judge(b)){
inttype=raf1.readInt();
intsize=raf1.readInt();
//System.out.println("size======"+size);
raf1.read(b1);
Strings3=newString(b1);
intfirstblock=raf1.readInt();
//System.out.println("tyep======"+type);
//System.out.println("firstblock========"+firstblock);
System.out.println(s3+""+"
"+""+size+""+s2);//System.out.println("ByteCount======"+ByteCount);
}
else{
raf1.skipBytes(26);
}
ByteCount+=32;
}
else{
break;
}
}
}
publicstaticbooleanjudge(byte[]b){
for(inti=0;iif(b[i]!
=0){
returnfalse;
}
else{
continue;
}
}
returntrue;
}
publicstaticvoidmd(Stringcmd){
Stringstr=cmd.substring(3);
try{
//System.out.println("strr==========="+str);
byte[]by={(byte)0xFFFF};
raf1=newRandomAccessFile(file,"rw");
raf1.seek(blockno*1024+block);
//System.out.println(!
sameName(str,blockno*1024+block));
//intn=blockno*1024+16;
if(!
sameName(str,blockno*1024+blockcount*2)){
//System.out.println("blcok====="+(blockno*1024+block));
block+=32;
FCBfcb=newFCB(str);
Calendarcal=Calendar.getInstance();
fcb.type=1;
fcb.size=1024;
Stringstr1=cal.get(Calendar.YEAR)+""+(cal.get(Calendar.MONTH)+1)+""+cal.get(Calendar.DAY_OF_MONTH)
+""+cal.get(Calendar.HOUR_OF_DAY)+""+cal.get(Calendar.MINUTE)+"-"+cal.get(Calendar.SECOND);
if(str1.length()>14){
str1=str1.substring(0,14);
}
while(str1.getBytes().length<14){
str1+='\u0000';
}
//System.out.println(str1.length());
fcb.cal=str1;
fcb.firstblock=firstblock;
//System.out.println("first======"+firstblock);
++firstblock;
//System.out.println("sencond++=="+firstblock);
//oos.writeObject(f);
raf1.write(fcb.getName().getBytes());
raf1.writeInt(fcb.getType());
raf1.writeInt(fcb.getSize());
//System.out.println("fcb.getCal().getBytes().length"+fcb.getCal().getBytes().length);
raf1.write(fcb.getCal().getBytes());
//System.out.println("fcb.getCal().getBytes()====="+fcb.getCal().getBytes().length);
raf1.writeInt(fcb.getFirstblock());
++times;
raf1.seek(f);
raf1.write(by);raf1.write(by);
f+=2;
//System.out.println(f);
raf1.close();
System.out.println("创建成功");
}
else{
System.out.println("此文件已经存在,请换个名字创建");
return;
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicstaticbooleansameName(Stringstr,intn){
try{
RandomAccessFileraf2=newRandomAccessFile(file,"r");
intByteCount=0;
byte[]b=newbyte[6];
raf2.seek(n);
while(true){
raf2.read(b);
Strings=newString(b);
//System.out.println("str========"+str);
//System.out.println("s======="+s);
//System.out.println(str+"="+str.length());
//System.out.println(s+"="+s.length());
//System.out.println("BYcoutn========="+ByteCount);
//System.out.println(compare(str,s));
if(ByteCount!
=1024){
if(compare(str,s)){
returntrue;
}
else{
raf2.skipBytes(26);
ByteCount+=32;
}
}
else{
returnfalse;
}
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnfalse;
}
publicstaticvoidcd(Stringcmd)throwsException{
Stringstr=cmd.substring(3);
raf1=newRandomAccessFile(file,"r");
//raf1.skipBytes(blockcount*2-1);
//Strings1="";
byte[]b=newbyte[6];
Strings4="";
if(str.equals("/")||str.equals("..")){
blockno=0;
current_directory="";
block=findBlock()*32+blockcount*2;
//System.out.println(block);
times=0;
return;
}
inta=blockno*1024+blockcount*2;
raf1.seek(a);
while(true){
raf1.read(b);
//System.out.println("blockcount====="+blockcount*2);
Strings1=newString(b);
if(s1.indexOf(str)!
=-1){
raf1.skipBytes(22);
intc=raf1.readInt();
blockno=c;
//System.out.println("blockno======="+blockno);
current_directory+=str;
current_directory+="/";
block-=times*32;
times=0;
//System.out.println("helloiaminhere");
break;
}
else{
//raf1.seek(blockcount*2+32);
raf1.skipBytes(26);
}
}
}
publicstaticintfindBlock()throwsException{
RandomAccessFileraf2=newRandomAccessFile(file,"rw");
raf2.seek(blockcount*2);
byte[]b=newbyte[6];
intblock1=0;
while(true){
raf2.read(b);
Stringstr=newString(b);
//System.out.println("str="+str);
//System.out.println("block1="+block1);
//System.out.println(judge(b));
if(!
judge(b)){
raf2.skipBytes(26);
++block1;
}
else{
returnblock1;
}
}
}
publicstaticvoidrd(Stringcmd)throwsException{
Stringstr=cmd.substring(3);
raf1=newRandomAccessFile(file,"rw");
raf1.seek(blockno*1024+blockcount*2);
byte[]b=newbyte[6];
byte[]b1=newbyte[14];
byte[]b2={0};
intByteCount=0;
//inttime=0;
//System.out.println(blockno*1024+16);
while(true){
raf1.read(b);
Strings2=newString(b);
//System.out.println(s2.indexOf(str)!
=-1);
if(ByteCount!
=1024){
if(s2.indexOf(str)!
=-1){
//if(comp