第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().length6){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(i0){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++DIR++size++s2);//System.out.println(ByteCount======+ByteCount);}else{raf1.skipBytes(26);}ByteCount+=32;}else{break;}}}publicstaticbooleanjudge(byte[]b){for(inti=0;ib.length;i++){if(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().length14){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()