一、设计目的
1、模拟DOS系统文件的建立和使用情况,理解磁盘文件的物理结构
2、深入理解文件的物理结构与存取方法之间的关系
二、设计要求
1、模拟设计DOS系统中磁盘文件的存储结构
假定磁盘存储空间共有l00个物理块,设计一个FAT表。FAT表可用一个一维数组定义,其中每一个数组元素与一个物理应。 当第i个元素为0时,表示第i盘块空闲;当第i个元素为-1时,其值表示一个文件结束标志;当第i个元素为其他数值时,其值表示该文件的下一物理块号。初始化时FAT标的所有元素均为0。另外,再设一个空闲块总数变量,记录系统还剩的空闲块数。它的值和FAT 表中值为0的元素个数应该一致。设计磁盘文件目录,每个文件保存:文件名、起始盘块号、长度(占盘块数)三个信息。
2、模拟设计文件存储时磁盘空间的分配过程
(1) 假定一个物理块的容量是1024B,要求设计一个程序,把文件的流式逻辑结构转换成链接物理结构:当用户要求将文件保存在磁盘上时,给出文件名及文件的长度,系统应能在磁盘上正确地为其分配存储空间,保存文件,改写FAT表和磁盘文件目录。这个程序形成一个键盘命令: write(文件名,文件长度)
(2) 要求设计另一个程序,当用户改写了文件后给出文件名及文件的长度,要求重新保存文件时,系统应能将文件原占有的盘块按其原有的逻辑顺序分配给该文件,保存文件。如果改写后的文件短于原文件,则回收它原来占有的倒数若干个盘块;如果改写后的文件长于原文件,则要为它增加分配若干个盘块。重新保存文件后要改写FAT表和磁盘文件目录。这个程序形成一个键盘命令: rwrite(文件名,文件长度)
3、模拟设计文件存储时磁盘空间的回收过程
要求设计第三个程序,当用户要求删除指定文件时,系统应能改写FAT表,回收该用户文件占用的磁盘空间,并删除该文件的 目录信息。这个程序形成一个键盘命令: dele(文件名)
设计上述三条指令,在文件保存、删除后显示FAT表和磁盘文件目录。在磁盘空间分配、回收的过程中,注意检查盘块 使用情况的数据一致性。
三.代码
1.OS_Test_4.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class OS_Test_4 {
- List<FATTable> fatTables = new ArrayList<FATTable>(); //all block;
- //empty block
- List<FATTable> fatTables_in = new ArrayList<FATTable>(); //using block;
- List<FileCatologue> fileCatologues = new ArrayList<FileCatologue>();
- List<File> files = new ArrayList<File>();
- int count = 0;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- boolean rw = true;
- public static void main(String[] args) {
- new OS_Test_4().getInfo();
- }
- public void getInfo() {
- boolean flag = true;
- String select;
- initial();
- showInfo();
- while(flag) {
- try {
- select = br.readLine();
- int index = Integer.parseInt(select);
- action(index);
- showInfo();
- } catch(NumberFormatException e) {
- System.out.println("the selection must be a number.......");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void action(int index) {
- switch(index) {
- case 1:
- //showInfo();
- showFAT();
- break;
- case 2:
- System.out.println("input a file into FAT.");
- try {
- System.out.println("input the name of the file:");
- String fname = br.readLine();
- System.out.println("input the size of the file(integer):");
- int size = Integer.parseInt(br.readLine());
- write(fname, size);
- //showInfo();
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case 3:
- System.out.println("rewrite a file in the fat.");
- rewrite();
- break;
- case 4:
- System.out.println("recycle a file in the fat.");
- System.out.println("input the name of the file you want to recycle:");
- String reName;
- try {
- reName = br.readLine();
- recycle(reName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case 5:
- System.out.println("System exited......");
- System.exit(0);
- break;
- default:
- break;
- }
- }
- public void showFAT() {
- //System.out.println("1:show the info of the FAT.");
- ListIterator<FATTable> fatIt = fatTables.listIterator();
- System.out.println("***********************************");
- System.out.println("the information of the FATTable:");
- //System.out.println("NO.:/tindex:");
- while(fatIt.hasNext()) {
- FATTable ft = fatIt.next();
- System.out.println("NO.:" +ft.getNo() + "/t" + "/t" + "index:" + ft.getI());
- }
- System.out.println("***********************************");
- }
- public void showFile() {
- ListIterator fileIt = fileCatologues.listIterator();
- System.out.println("the info of the file:");
- System.out.println("Name:/tSize:");
- while(fileIt.hasNext()) {
- FileCatologue file = (FileCatologue) fileIt.next();
- System.out.println(file.getName() + "/t" + file.getSize());
- }
- }
- public void showInfo() {
- System.out.println("Select the follow choices you want to do:");
- System.out.println("1:show the info of the FAT.");
- System.out.println("2:input a file into the FAT.");
- System.out.println("3:rewrite a file in the FAT.");
- System.out.println("4:recycle a file in the FAT.");
- System.out.println("5:exit.");
- System.out.println("***********************************");
- System.out.println("the count of the FAT block: " + count );
- System.out.println("***********************************");
- showFile();
- System.out.println("the count of the files: " + fileCatologues.size() );
- System.out.println("***********************************");
- }
- public void initial() {
- for(int i=0; i<20; i++) { //FAT表初始化;
- addFATTable(0, i);
- }
- }
- public void addFATTable(int i, int no) {
- FATTable fatTable = new FATTable(i, no);
- fatTables.add(fatTable);
- count ++;
- }
- public void recycle(String fname) {
- boolean flag = true;
- boolean fileFlag = true;
- int start = -1;
- int size = 0;
- ListIterator<FileCatologue> fileIt = fileCatologues.listIterator();
- ListIterator<FileCatologue> fileIt_del = fileCatologues.listIterator();
- FileCatologue fc_del = null;
- boolean findFlag = false;
- while(flag) {
- if(!fileIt.hasNext()) {
- flag = false;
- }
- else {
- FileCatologue file = fileIt.next();
- if(file.getName().equals(fname)) {
- start = file.getStart();
- size = file.getSize();
- }
- }
- }
- if(start > -1) {
- FATTable ft_del = fatTables.get(start);
- int index_del = ft_del.getI();
- while(index_del != -1) {
- ft_del.setI(0);
- count ++;
- ft_del = fatTables.get(index_del);
- index_del = ft_del.getI();
- }
- ft_del.setI(0);
- findFlag = true;
- }
- else {
- System.out.println("The file not found......");
- }
- if(findFlag) {
- count ++;
- while(fileFlag) {
- if(!fileIt_del.hasNext()) {
- fileFlag = false;
- }
- else {
- fc_del = fileIt_del.next();
- if(fc_del.getName().equals(fname)) {
- fileFlag = false;
- }
- }
- }
- if(fc_del != null) {
- fileCatologues.remove(fc_del);
- }
- }
- }
- public void rewrite() {
- boolean find = true;
- boolean findFlag = false;
- ListIterator fileIt = fileCatologues.listIterator();
- try {
- System.out.println("input the old name of the file:");
- String oName = br.readLine();
- System.out.println("input the new name of the file:");
- String nName = br.readLine();
- System.out.println("input the new size of the file:");
- int nSize = Integer.parseInt(br.readLine());
- while(find) {
- if(!fileIt.hasNext()) {
- find = false;
- }
- else {
- FileCatologue file = (FileCatologue) fileIt.next();
- if(file.getName().equals(oName)) {
- recycle(oName);
- write(nName, nSize);
- find = false;
- findFlag = true;
- }
- }
- }
- if(!findFlag) {
- System.out.println("fail to rewrite......");
- }
- } catch(NumberFormatException e) {
- System.out.println("the size of the file must be a number.");
- System.exit(0);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void write(String fname, int fsize) {
- int start = -1;
- boolean find = true;
- int fsize_1 = fsize;
- int countFile = 0;
- List<FATTable> fatTables_1 = new ArrayList<FATTable>();
- ListIterator<FATTable> fatIt = fatTables.listIterator();
- while(find) {
- if(!fatIt.hasNext()) {
- find = false;
- }
- else {
- FATTable ft = fatIt.next();
- if(ft.getI() == 0 ) {
- countFile ++;
- fatTables_1.add(ft);
- }
- }
- }
- if((fsize % 1024) != 0) {
- fsize = fsize/1024 + 1;
- }
- else {
- fsize = fsize/1024;
- }
- if((countFile > fsize || countFile == fsize) && fsize > 0) {
- if(fsize == 1) {
- int no = fatTables_1.get(0).getNo();
- ListIterator<FATTable> fatIt_1 = fatTables.listIterator();
- boolean flag = true;
- while(flag) {
- if(!fatIt_1.hasNext()) {
- flag = false;
- }
- else {
- FATTable ft = fatIt_1.next();
- if(ft.getNo() == no) {
- ft.setI(-1);
- count = count - fsize;
- start = no;
- flag = false;
- }
- }
- }
- }
- else {
- for(int i=1; i<fsize; i++) {
- boolean flag = true;
- ListIterator<FATTable> fatIt_1 = fatTables.listIterator();
- FATTable ft = fatTables_1.get(i-1);
- FATTable ft_n = fatTables_1.get(i);
- int no = ft.getNo();
- int no_n = ft_n.getNo();
- if(start == -1) {
- start = no;
- }
- while(flag) {
- if(!fatIt_1.hasNext()) {
- flag = false;
- }
- else {
- FATTable ft_1 = fatIt_1.next();
- if(ft_1.getNo() == no) {
- ft_1.setI(no_n);
- flag = false;
- //count --;
- }
- }
- }
- }
- int no_l = fatTables_1.get(fsize-1).getNo();
- ListIterator<FATTable> fatIt_2 = fatTables.listIterator();
- while(fatIt_2.hasNext()) {
- FATTable ft = fatIt_2.next();
- if(ft.getNo() == no_l) {
- ft.setI(-1);
- count = count - fsize;
- }
- }
- }
- fileCatologues.add(new FileCatologue(fname, start, fsize_1));
- System.out.println("the file has been saved.......");
- System.out.println("************************************************************");
- }
- else {
- System.out.println("the size of the file is too big or the memory have been used up......");
- }
- }
- }
2. File.java
- public class File {
- String fname = null;
- int no = -1;
- public File(String fname, int no) {
- this.no = no;
- this.fname = fname;
- }
- public String getName() {
- return fname;
- }
- public void setName(String fname) {
- this.fname = fname;
- }
- public int getNo() {
- return no;
- }
- public void setNo(int no) {
- this.no = no;
- }
- }
3. FileCatologue.java
- public class FileCatologue {
- //设计磁盘文件目录,每个文件保存:文件名、起始盘块号、长度(占盘块数)三个信息
- String fname;
- int start = 0;
- int size = 0;
- public FileCatologue(String fname, int start, int size) {
- this.fname = fname;
- this.start = start;
- this.size = size;
- }
- public String getName() {
- return fname;
- }
- public int getStart() {
- return start;
- }
- public int getSize() {
- return size;
- }
- }
4. FATTable.java
- public class FATTable {
- int i = -1;
- int no = -1;
- int index = -1;
- public FATTable(int i, int no) {
- this.i = i;
- this.no = no;
- }
- public int getI() {
- return i;
- }
- public void setI(int i) {
- this.i = i;
- }
- public int getNo() {
- return no;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public int getIndex() {
- return index;
- }
- }