disk.java
来自「一个简单的操作系统模拟程序 java+swing 可进行一般的文件操作 进」· Java 代码 · 共 151 行
JAVA
151 行
/*
* Disk.java,ver 0.1.1
*
* Created on 2006年11月30日, 下午12:26
*
*This Class is used to simulate the HardDisk for the system.
*
*History :2006年11月30日,ver 0.1.0 first release.
* 2006年12月27日,ver 0.1.1 second release
* add the saveToFile method
*
*/
package ossimulation;
import java.io.*;
/**
*
* @author Decell.Zhou
*/
public class Disk {
private char[][] disk;
private int blockIndex;
/** Creates a new instance of Disk */
public Disk() {
//creat a disk.this disk has 128 Block,each block has 64 Byte.
disk = new char[128][64];
//disk initialization,0 stands for unused block
for(int i = 0;i <= 127 ;i++){
for(int j = 0; j <= 63;j++){
disk[i][j] = 0;
}
}
//block one and block two is used for store the file allocation table.
disk[0][0] = 1;
disk[0][1] = 1;
}
/**Search for the free block in the disk ,return its index*/
public int findNextFree(){
//search for free block
outer:
for(int i = 0;i <= 1;i++){
for(int j = 0;j <= 63;j++){
if(disk[i][j] == 0){
blockIndex = j + i * 64;
break outer;
}
}
}
//return the index
return blockIndex;
}
/**Read one block in the disk*/
public char[] readBlock(int argIndex){
return disk[argIndex];
}
/**Write something to a bolck*/
public void writeBlock(char[] argSource,int argIndex){
//copy the content from the source to disk
for(int i = 0;i <=63;i++){
disk[argIndex][i] = argSource[i];
}
//set the corresponding Byte in FAT ,indicated that the block id in used
if(argIndex >= 64){
disk[1][argIndex - 64] = 1;
} else {
disk[0][argIndex] = 1;
}
}
/**Read the file allocation table from the disk*/
public char[] readFAT(){
char[] fileAlocationTable = new char[128];
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 63; j++){
fileAlocationTable[i * 64 + j] = disk[i][j];
}
}
return fileAlocationTable;
}
/**Erase one block*/
public void eraseBlock(int argIndex){
//clear all the imformation in the target block
for(int i = 0; i <= 63; i++){
disk[argIndex][i] = 0;
}
//set the corresponding FAT Byte to unused
if(argIndex >= 64){
disk[1][argIndex - 64] = 0;
} else {
disk[0][argIndex] = 0;
}
}
/**save the contend of the disk to the file*/
public void saveToFile() throws IOException{
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("./disk.out")));
for(int i = 0;i <= 127;i ++){
writer.println(disk[i]);
}
writer.close();
}
/**Existed only for testing this class*/
static public void main(String[] args){
Disk diskEntity = new Disk();
// test the findNextFree function
System.out.println(diskEntity.findNextFree());
//test the writeBlockfunction
char[] content = new char[64];
for(int i = 0;i <= 63;i++){
content[i] = Integer.toString(i).charAt(0);
}
content[0] = 'A';
for(int i = 2;i <= 65;i ++){
diskEntity.writeBlock(content,i);
}
diskEntity.writeBlock(content,127);
System.out.println(diskEntity.findNextFree());
//test the erase block function
diskEntity.eraseBlock(4);
System.out.println(diskEntity.findNextFree());
//test the readFAT function
char[] FAT = new char[128];
FAT = diskEntity.readFAT();
for(int i = 0;i <= 127; i++){
System.out.print(FAT[i]);
}
System.out.println();
//test the readBlock funtcion
content = diskEntity.readBlock(127);
for(int i = 0;i <= 63;i++){
System.out.print(content[i]);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?