📄 harddiskmanager.java
字号:
/*******************************************************
*硬盘管理器
********************************************************/
package Manager;
import java.io.*;
import harddisk.*;
import Global.*;
import myException.*;
import java.util.*;
public class HardDiskManager
{
private int newIndex;
public HardDiskManager()
{}
/*************************************************
*功能描述: 把文件读入虚拟硬盘
*参数说明: filename: 文件名及其路径
*抛出异常: IOException:找不到指定文件出错 myException:虚拟硬盘没有足够空间
**************************************************/
public void GetFile(String filename)
throws IOException,myException
{
newIndex = this.Find();
String name = filename.substring(filename.lastIndexOf("\\")+1);
if (newIndex != -1)
{
try
{
int index = 0;
String buff;
int table[] = new int[3];
FileReader file = new FileReader(filename);
BufferedReader in = new BufferedReader(file);
while((buff=in.readLine())!=null)
{
buff = ""+index+" "+buff;
Global.HARDDISK.Add(newIndex,buff);
index++;
}
table[0] = this.createID();
table[1] = newIndex;
table[2] = Global.HARDDISK.size(newIndex);
Global.HDTABLE.add(table,name);
}
catch(IOException e){throw new IOException();}
catch(myException e){throw new myException("作业表已经达到最大记录数,不可以再添加了!");}
}
else throw new myException("虚拟硬盘已经达到最大容量!");
}
/****************************************************
*功能描述: 找到空的链表
*返回值: 链表的序号
****************************************************/
private int Find()
{
for (int i=0;i<Global.HD_SIZE ;i++ )
{
if (Global.HARDDISK.size(i) == 0)
{
return i;
}
}
return -1;
}
/****************************************************
*功能描述: 生成一个作业ID
*返回值: 生成的作业ID
****************************************************/
private int createID()
{
Random random = new Random();
int id = random.nextInt();
while (Global.HDTABLE.findID(id) != -1)
{
id = random.nextInt();
}
return id;
}
/****************************************************
*功能描述: 从虚拟硬盘中移除一个作业并把该作业在作业表中的值高为初始值
*参数描述: id:作业ID
*抛出异常: 当在作业表中找不到对应的ID时抛出异常
****************************************************/
public void RemoveFile(int id)
throws myException
{
int table_index,harddisk_index;
table_index = Global.HDTABLE.findID(id);
if(table_index != -1)
{
harddisk_index = Global.HDTABLE.getValue(table_index,1);
Global.HARDDISK.RemoveJob(harddisk_index);
Global.HDTABLE.Remove(table_index);
}
else throw new myException("作业ID错误,找不到此ID");
}
/*********************************************
*功能描述: 返回某作业的大小
*参数说明: id:作业序号
**********************************************/
public int Size(int id)
{
return Global.HARDDISK.size(id);
}
/*********************************************
*功能描述: 获取作业ID的index地址的值
*参数描述: id:作业的序号 offset:地址
**********************************************/
public String Get(int id,int offset)
{
return Global.HARDDISK.Get(id,offset);
}
/*********************************************
*功能描述: 获取最后一次插入的作业的序号
*返回值: 最后一次插入的作业的序号
**********************************************/
public int getIndex()
{
return newIndex;
}
/**********************************************
*功能描述: 获取作业的ID
*参数说明: 作业的序号
**********************************************/
public int getID(int index)
{
return Global.HDTABLE.getID(index);
}
/*********************************************
*功能描述: 获取文件名
*参数说明: index:文件序号
*********************************************/
public String getFileName(int index)
{
return Global.HDTABLE.getName(index);
}
/*********************************************
*功能描述: 获取table表中第二字段的值
**********************************************/
public int getIndex(int id)
{
return Global.HDTABLE.getValue(id,1);
}
/**********************************************
*功能描述: 通过作业ID找到作业在硬盘中的链序号
*参数说明: 作业ID
*************************************************/
public int GetIndex(int id)
{
return Global.HDTABLE.findIndex(id);
}
/*************************************************
*功能描述: 返回当前硬盘的作业数
**************************************************/
public int GetUsedSize()
{
return Global.HDTABLE.UsedSize();
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -