mystuinfowriter.java

来自「学生信息读系统(根据学生的学号从存储系统读取学生信息): 姓名」· Java 代码 · 共 93 行

JAVA
93
字号
//A inherited class of StudentInfoWriter.It writes student's
//information into a local file.
import java.io.File;
import java.io.RandomAccessFile;

public class MyStuInfoWriter extends StudentInfoWriter
{
	
	//This boolean value show if writing successfully.
	//While this boolean value is true and writeInfos(String) is true.
	//This writing is just successful.
	public boolean  isSuccessful=false;
	
	//The file storing information
	protected File infors=null;
	
	//The file storing information's index represents by
	// a Hashtable.
	protected File indexs=null;
    
	//The index of the specified student's informaiont writing into file infors.
	private int index=0; 
	
	//The length of the specified student's information writing into file infors.
	private int length=0;
	
	//Write positoin.
	private int infoPosition=0;
	
	private int indePosition=0;
	
	RandomAccessFile w1=null;
	
	RandomAccessFile w2=null;
	
	//Construct method.
	//@param file the name of file for writing information.
	public MyStuInfoWriter(String file)
	{
	     infors=new File("infos"+"\\"+file+".ino");
	     indexs=new File("infos"+"\\"+file+".ind");
	     index=(int)infors.length();
	     
	     infoPosition=(int)infors.length();
	     indePosition=(int)indexs.length();
	     
	     try
	     {
	         w1=new RandomAccessFile(infors,"rw");
		     w2=new RandomAccessFile(indexs,"rw");
		     isSuccessful=true;
		 }
		 catch(Exception e)
		 {
		     	isSuccessful=false;
		 }
	}
	
	//Implement the abstract method.
	public void write(String s) throws Exception
 	{
 		length=s.length();
 		w1.seek(infoPosition);
		w1.writeChars(s);
	    w2.seek(indePosition);
	    String s1=change();
		w2.writeChars(s1);
		index+=2*length;
		
		infoPosition+=length*2;
		indePosition+=(s1.length())*2;
	}
	
	//Close this writer.
	public void close()
	{
		try
		{
	        w1.close();
	        w2.close();
	    }
	    catch(Exception e)
	    {
	    	
	    }
	}
	
	//Get the String.
	protected String change()
	{
		return new String(stuID+"/"+Integer.toString(index)+"/"+Integer.toString(length*2)+"/");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?