现在的位置: 首页 > 综合 > 正文

模拟DOS系统文件的物理结构和管理使用

2013年09月01日 ⁄ 综合 ⁄ 共 14056字 ⁄ 字号 评论关闭

 

一、设计目的
        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

Code:
  1.   
  2. import java.io.BufferedReader;   
  3. import java.io.IOException;   
  4. import java.io.InputStreamReader;   
  5. import java.util.*;   
  6.   
  7. public class OS_Test_4 {   
  8.     List<FATTable> fatTables = new ArrayList<FATTable>();                           //all block;               
  9.     //empty block   
  10.     List<FATTable> fatTables_in = new ArrayList<FATTable>();                        //using  block;   
  11.     List<FileCatologue> fileCatologues = new ArrayList<FileCatologue>();   
  12.     List<File> files = new ArrayList<File>();   
  13.     int count = 0;   
  14.     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  15.     boolean rw = true;    
  16.     public static void main(String[] args) {   
  17.         new OS_Test_4().getInfo();    
  18.     }   
  19.        
  20.     public void getInfo() {   
  21.         boolean flag = true;   
  22.         String select;   
  23.            
  24.         initial();   
  25.         showInfo();                        
  26.         while(flag) {   
  27.             try {   
  28.                 select = br.readLine();   
  29.                 int index = Integer.parseInt(select);   
  30.                 action(index);   
  31.                 showInfo();    
  32.             } catch(NumberFormatException e) {   
  33.                 System.out.println("the selection must be a number.......");   
  34.             } catch (IOException e) {   
  35.                 e.printStackTrace();   
  36.             }   
  37.         }   
  38.            
  39.     }   
  40.        
  41.     public void action(int index) {   
  42.         switch(index) {   
  43.             case 1:                
  44.                 //showInfo();   
  45.                 showFAT();   
  46.                 break;   
  47.             case 2:   
  48.                 System.out.println("input a file into FAT.");   
  49.                 try {   
  50.                     System.out.println("input the name of the file:");   
  51.                     String fname = br.readLine();   
  52.                     System.out.println("input the size of the file(integer):");   
  53.                     int size = Integer.parseInt(br.readLine());   
  54.                     write(fname, size);   
  55.                     //showInfo();   
  56.                 } catch (IOException e) {   
  57.                     e.printStackTrace();   
  58.                 }   
  59.                 break;   
  60.             case 3:   
  61. System.out.println("rewrite a file in the fat.");   
  62.                 rewrite();   
  63.                 break;   
  64.             case 4:   
  65. System.out.println("recycle a file in the fat.");   
  66.                 System.out.println("input the name of the file you want to recycle:");   
  67.                 String reName;   
  68.                 try {   
  69.                     reName = br.readLine();   
  70.                     recycle(reName);   
  71.                 } catch (IOException e) {   
  72.                     e.printStackTrace();   
  73.                 }                  
  74.                 break;   
  75.             case 5:                
  76.                 System.out.println("System exited......");   
  77.                 System.exit(0);   
  78.                 break;   
  79.             default:   
  80.                 break;   
  81.         }   
  82.     }   
  83.        
  84.     public void showFAT() {   
  85. //System.out.println("1:show the info of the FAT.");   
  86.         ListIterator<FATTable> fatIt = fatTables.listIterator();   
  87.         System.out.println("***********************************");   
  88.         System.out.println("the information of the FATTable:");   
  89.         //System.out.println("NO.:/tindex:");   
  90.         while(fatIt.hasNext()) {   
  91.             FATTable ft = fatIt.next();   
  92.             System.out.println("NO.:" +ft.getNo() + "/t" + "/t" + "index:" + ft.getI());   
  93.         }   
  94.         System.out.println("***********************************");   
  95.     }   
  96.        
  97.     public void showFile() {   
  98.         ListIterator fileIt = fileCatologues.listIterator();   
  99.         System.out.println("the info of the file:");   
  100.         System.out.println("Name:/tSize:");   
  101.         while(fileIt.hasNext()) {   
  102.             FileCatologue file = (FileCatologue) fileIt.next();   
  103.             System.out.println(file.getName() + "/t" + file.getSize());   
  104.         }   
  105.     }   
  106.            
  107.     public void showInfo() {   
  108.         System.out.println("Select the follow choices you want to do:");   
  109.         System.out.println("1:show the info of the FAT.");   
  110.         System.out.println("2:input a file into the FAT.");   
  111.         System.out.println("3:rewrite a file in the FAT.");   
  112.         System.out.println("4:recycle a file in the FAT.");   
  113.         System.out.println("5:exit.");   
  114.         System.out.println("***********************************");   
  115.         System.out.println("the count of the FAT block:  " + count );   
  116.         System.out.println("***********************************");   
  117.         showFile();   
  118.         System.out.println("the count of the files:  " + fileCatologues.size() );   
  119.         System.out.println("***********************************");   
  120.     }   
  121.        
  122.     public void initial() {                            
  123.         for(int i=0; i<20; i++) {                   //FAT表初始化;   
  124.             addFATTable(0, i);   
  125.         }    
  126.     }   
  127.        
  128.     public void addFATTable(int i, int no) {   
  129.         FATTable fatTable = new FATTable(i, no);   
  130.         fatTables.add(fatTable);   
  131.         count ++;   
  132.     }   
  133.        
  134.     public void recycle(String fname) {   
  135.         boolean flag = true;   
  136.         boolean fileFlag = true;   
  137.         int start = -1;   
  138.         int size = 0;   
  139.         ListIterator<FileCatologue> fileIt = fileCatologues.listIterator();   
  140.         ListIterator<FileCatologue> fileIt_del = fileCatologues.listIterator();   
  141.         FileCatologue fc_del = null;   
  142.         boolean findFlag = false;   
  143.         while(flag) {   
  144.             if(!fileIt.hasNext()) {   
  145.                 flag = false;   
  146.             }   
  147.             else {   
  148.                 FileCatologue file = fileIt.next();   
  149.                 if(file.getName().equals(fname)) {   
  150.                     start = file.getStart();   
  151.                     size = file.getSize();   
  152.                 }   
  153.             }   
  154.         }   
  155.         if(start > -1) {   
  156.             FATTable ft_del = fatTables.get(start);   
  157.             int index_del = ft_del.getI();   
  158.             while(index_del != -1) {   
  159.                 ft_del.setI(0);   
  160.                 count ++;   
  161.                 ft_del = fatTables.get(index_del);         
  162.                 index_del = ft_del.getI();   
  163.             }   
  164.             ft_del.setI(0);   
  165.             findFlag = true;   
  166.         }   
  167.         else {   
  168.             System.out.println("The file not found......");   
  169.         }   
  170.         if(findFlag) {   
  171.             count ++;   
  172.             while(fileFlag) {   
  173.                 if(!fileIt_del.hasNext()) {   
  174.                     fileFlag = false;   
  175.                 }   
  176.                 else {   
  177.                     fc_del = fileIt_del.next();   
  178.                     if(fc_del.getName().equals(fname)) {   
  179.                         fileFlag = false;   
  180.                     }   
  181.                 }   
  182.             }   
  183.             if(fc_del != null) {   
  184.                 fileCatologues.remove(fc_del);   
  185.             }   
  186.         }   
  187.     }      
  188.        
  189.     public void rewrite() {        
  190.         boolean find = true;   
  191.         boolean findFlag = false;   
  192.         ListIterator fileIt = fileCatologues.listIterator();   
  193.         try {   
  194.             System.out.println("input the old name of the file:");   
  195.             String oName = br.readLine();   
  196.             System.out.println("input the new name of the file:");   
  197.             String nName = br.readLine();   
  198.             System.out.println("input the new size of the file:");   
  199.             int nSize = Integer.parseInt(br.readLine());   
  200.             while(find) {   
  201.                 if(!fileIt.hasNext()) {   
  202.                     find = false;   
  203.                 }   
  204.                 else {   
  205.                     FileCatologue file = (FileCatologue) fileIt.next();   
  206.                     if(file.getName().equals(oName)) {   
  207.                         recycle(oName);   
  208.                         write(nName, nSize);   
  209.                         find = false;   
  210.                         findFlag = true;   
  211.                     }   
  212.                 }   
  213.             }   
  214.             if(!findFlag) {   
  215.                 System.out.println("fail to rewrite......");   
  216.             }   
  217.         } catch(NumberFormatException e) {   
  218.             System.out.println("the size of the file must be a number.");   
  219.             System.exit(0);   
  220.         } catch (IOException e) {   
  221.             e.printStackTrace();   
  222.         }   
  223.            
  224.     }   
  225.        
  226.     public void write(String fname, int fsize) {   
  227.         int start = -1;   
  228.         boolean find = true;   
  229.         int fsize_1 = fsize;    
  230.         int countFile = 0;   
  231.         List<FATTable> fatTables_1 = new ArrayList<FATTable>();     
  232.         ListIterator<FATTable> fatIt = fatTables.listIterator();           
  233.         while(find) {   
  234.             if(!fatIt.hasNext()) {   
  235.                 find = false;   
  236.             }   
  237.             else {   
  238.                 FATTable ft = fatIt.next();   
  239.                 if(ft.getI() == 0 ) {   
  240.                     countFile ++;   
  241.                     fatTables_1.add(ft);   
  242.                 }   
  243.             }   
  244.         }   
  245.         if((fsize % 1024) != 0) {   
  246.             fsize = fsize/1024 + 1;   
  247.         }   
  248.         else {   
  249.             fsize = fsize/1024;   
  250.         }   
  251.         if((countFile > fsize || countFile == fsize) && fsize > 0) {   
  252.                
  253.             if(fsize == 1) {   
  254.                 int no = fatTables_1.get(0).getNo();   
  255.                 ListIterator<FATTable> fatIt_1 = fatTables.listIterator();   
  256.                 boolean flag = true;               
  257.                 while(flag) {   
  258.                     if(!fatIt_1.hasNext()) {   
  259.                         flag = false;   
  260.                     }   
  261.                     else {   
  262.                         FATTable ft = fatIt_1.next();   
  263.                         if(ft.getNo() == no) {   
  264.                             ft.setI(-1);   
  265.                             count = count - fsize;   
  266.                             start = no;   
  267.                             flag = false;   
  268.                         }   
  269.                     }   
  270.                 }      
  271.             }      
  272.             else {   
  273.                                
  274.                 for(int i=1; i<fsize; i++) {   
  275.                     boolean flag = true;   
  276.                     ListIterator<FATTable> fatIt_1 = fatTables.listIterator();   
  277.                     FATTable ft = fatTables_1.get(i-1);   
  278.                     FATTable ft_n = fatTables_1.get(i);   
  279.                     int no = ft.getNo();   
  280.                     int no_n = ft_n.getNo();   
  281.                     if(start == -1) {   
  282.                         start = no;   
  283.                     }   
  284.                     while(flag) {   
  285.                         if(!fatIt_1.hasNext()) {   
  286.                             flag = false;   
  287.                         }   
  288.                         else {   
  289.                             FATTable ft_1 = fatIt_1.next();   
  290.                             if(ft_1.getNo() == no) {   
  291.                                 ft_1.setI(no_n);   
  292.                                 flag = false;   
  293.                                 //count --;   
  294.                             }   
  295.                         }   
  296.                     }                      
  297.                 }          
  298.                 int no_l = fatTables_1.get(fsize-1).getNo();   
  299.                 ListIterator<FATTable> fatIt_2 = fatTables.listIterator();   
  300.                 while(fatIt_2.hasNext()) {   
  301.                     FATTable ft = fatIt_2.next();   
  302.                     if(ft.getNo() == no_l) {   
  303.                         ft.setI(-1);   
  304.                         count = count - fsize;   
  305.                     }   
  306.                 }   
  307.                                    
  308.             }   
  309.             fileCatologues.add(new FileCatologue(fname, start, fsize_1));   
  310.             System.out.println("the file has been saved.......");   
  311.             System.out.println("************************************************************");   
  312.             }   
  313.         else {   
  314.             System.out.println("the size of the file is too big or the memory have been used up......");   
  315.         }   
  316.     }   
  317. }   
  318.   
  319.   

2.  File.java

Code:
  1.   
  2. public class File {   
  3.     String fname = null;   
  4.     int no = -1;   
  5.        
  6.     public File(String fname, int no) {   
  7.         this.no = no;   
  8.         this.fname = fname;   
  9.     }   
  10.     public String getName() {   
  11.         return fname;   
  12.     }   
  13.     public void setName(String fname) {   
  14.         this.fname = fname;   
  15.     }   
  16.     public int getNo() {   
  17.         return no;   
  18.     }   
  19.     public void setNo(int no) {   
  20.         this.no = no;   
  21.     }   
  22.        
  23. }   

3.   FileCatologue.java

Code:
  1.   
  2. public class FileCatologue {   
  3.     //设计磁盘文件目录,每个文件保存:文件名、起始盘块号、长度(占盘块数)三个信息   
  4.     String fname;   
  5.     int start = 0;   
  6.     int size = 0;    
  7.        
  8.     public FileCatologue(String fname, int start, int size) {   
  9.         this.fname = fname;   
  10.         this.start = start;   
  11.         this.size = size;   
  12.     }   
  13.        
  14.     public String getName() {   
  15.         return fname;   
  16.     }   
  17.     public int getStart() {   
  18.         return start;   
  19.     }   
  20.     public int getSize() {   
  21.         return size;   
  22.     }   
  23. }   

4.  FATTable.java

Code:
  1.   
  2. public class FATTable {   
  3.     int i = -1;   
  4.     int no = -1;   
  5.     int index = -1;   
  6.        
  7.     public FATTable(int i, int no) {   
  8.         this.i = i;   
  9.         this.no = no;   
  10.     }   
  11.     public int getI() {   
  12.         return i;   
  13.     }   
  14.     public void setI(int i) {   
  15.         this.i = i;   
  16.     }   
  17.     public int getNo() {   
  18.         return no;   
  19.     }   
  20.     public void setIndex(int index) {   
  21.         this.index = index;   
  22.     }   
  23.     public int getIndex() {   
  24.         return index;   
  25.     }   
  26. }   

 

 

抱歉!评论已关闭.