📄 mystuinforeader.java
字号:
//A inherit class of StudentInfoReader.It reads information
//of a student from a local file.
import java.io.File;
import java.io.RandomAccessFile;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MyStuInfoReader extends StudentInfoReader
{
//Show if this reading is successful.
//Only this boolean value and readInfos(String) are true
//This read will successful.
public boolean canRead=false;
//The information file
protected File infors=null;
//The indexs file.
protected File indexs=null;
//The index of the ID's information in the file infors to be read.
private int index=0;
//The length of the ID's information in the file infors to be read.
private int length=0;
RandomAccessFile ra=null;
FileInputStream fin=null;
FileChannel fc=null;
//param file the file name for reading.
public MyStuInfoReader(String file)
{
infors=new File("infos"+"\\"+file+".ino");
indexs=new File("infos"+"\\"+file+".ind");
if(!infors.exists()||!indexs.exists())
{
canRead=false;
}
else
{
try
{
ra=new RandomAccessFile(infors,"r");
fin=new FileInputStream(indexs);
canRead=true;
}
catch(Exception e)
{
canRead=false;
}
}
}
//inherit the abstract method.
public char[] read(String stuID) throws Exception
{
setValue(stuID);
char[] chars=new char[length/2];
ra.seek(index);
for(int i=0;i<chars.length;i++)
{
chars[i]=ra.readChar();
}
return chars;
}
//Get the index and length values of the specified stuID's information
//in file infors from file indexs.
protected void setValue(String id) throws Exception
{
fc=fin.getChannel();
ByteBuffer b=ByteBuffer.allocate((int)fc.size());
fc.read(b);
String[] s1=new String[2];
StringBuffer s=new StringBuffer();
int i=0;
b.position(0);
while(b.position()<b.limit())
{
char c=b.getChar();
i+=2;
b.position(i);
if(c=='/')
{
if(s.toString().equals(id))
{
s=null;
s=new StringBuffer();
int j=0;
while(j<2)
{
c=b.getChar();
if(c=='/')
{
s1[j]=s.toString();
s=null;
s=new StringBuffer();
j++;
}
else
{
s.append(c);
}
}
break;
}
else
{
s=null;
s=new StringBuffer();
}
}
else
{
s.append(c);
}
}
index=Integer.decode(s1[0]).intValue();
length=Integer.decode(s1[1]).intValue();
fc.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -