📄 fileio.java
字号:
file.writeFloat(0);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
file.close();
}
catch(Exception e)
{
System.out.println("File can't close properly!");
}
}
}
}
public int getRecordNumber()
{
try
{
file = new RandomAccessFile(f,"rw");
for(int i=1;i <= 100;i++)
{
file.seek((long)((i - 1)*Record.RECORD_LENGTH) );
if (file.readInt() != 0 )//Means the record is existing
{
recordNumber ++;
file.seek((long)( (i - 1)*Record.RECORD_LENGTH + 24) );
totalScore += file.readFloat();
}
}
}
catch (Exception e)
{
e.printStackTrace();
recordNumber = 0;
}
finally
{
try
{
file.close();
}
catch(Exception e)
{
System.out.println("File can't close properly!");
}
}
return recordNumber;
}
public float getAverage()
{
if (recordNumber == 0)
{
JOptionPane.showMessageDialog(null,"No records in the file",
"Error",JOptionPane.ERROR_MESSAGE);
return 0;
}
else
{
DecimalFormat formatter = new DecimalFormat("0.00");
return Float.parseFloat( (formatter.format(totalScore/recordNumber)) );
}
}
public String getFilePath()
{
return filePath;
}
public void addRecord(Record record)
{
try
{
file = new RandomAccessFile(f,"rw");
//Write the record into the file
record.write(file);
}
catch(IOException ie)
{
ie.printStackTrace();
}
finally
{
try
{
file.close();
}
catch(Exception e)
{
System.out.println("File can't close properly!");
}
}
}
}
class Record
{
public static final int RECORD_LENGTH = 28; //id:4bytes;name:20bytes;score:4bytes.So 28 bytes total for one record
private String id;
private String name;
private float score;
//Input data should be check before it used
public boolean setID(String id)
{
if (id.equals(""))
{
JOptionPane.showMessageDialog(null,"ID should not be empty",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
else if (id.length() != 3)
{
JOptionPane.showMessageDialog(null,"Invalid ID number",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
//ID number'scope is 000--100
else if(Integer.parseInt(id) < 0 && Integer.parseInt(id) > 100)
{
JOptionPane.showMessageDialog(null,"Invalid ID number","Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
else
{
this.id = id;
return true;
}
}
//Input data should be check before it used
public boolean setName(String name)
{
if (name.equals(""))
{
JOptionPane.showMessageDialog(null,"Name should not be empty",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
//The length of the name must be within 20 chars
else if (name.length() > 20)
{
JOptionPane.showMessageDialog(null,"The length of the name must be within 20 chars",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
else
{
while(name.length() < 20) //Make the name's length is 20
name += " ";
this.name = name;
return true;
}
}
//Input data should be check before it used
public boolean setScore(String score)
{
if (score.equals(""))
{
JOptionPane.showMessageDialog(null,"Score should not be empty",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
else if (Float.parseFloat(score) < 0 && Float.parseFloat(score) > 100)
{
JOptionPane.showMessageDialog(null,"Score should be within 0 to 100",
"Invalid Input",JOptionPane.ERROR_MESSAGE);
return false;
}
//Score should be within 0 to 100
else
{
this.score = Float.parseFloat(score);
return true;
}
}
public int getID()
{
return Integer.parseInt(id);
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public void write(RandomAccessFile file)
{
boolean shouldWrite = true;
try
{ //Locate the pointer at the specified position
//The head of the file is used to store "recordNumber"(int) and "totalScore"(float).
//So the first record is begin at the 8th byte
file.seek((long)((Integer.parseInt(id)-1) * RECORD_LENGTH ) );
//If the record has all ready existed
if (file.readInt() == Integer.parseInt(id) )
{
int val = JOptionPane.showOptionDialog(null,"记录已经存在,要替换它 吗?","保存为",
JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null);
if (val == JOptionPane.OK_OPTION) //Press OK to override the record
shouldWrite = true;
else if (val == JOptionPane.CANCEL_OPTION) //Press Cancel to give up override the record
shouldWrite = false;
}
//Should write the record into the file
if (shouldWrite)
{
file.seek((long)( (Integer.parseInt(id)-1) * RECORD_LENGTH ) );
//Write the ID number into the file
file.writeInt(Integer.parseInt(id));
//Write the name into the file
byte[] buf = new byte[20];
name.getBytes(0,name.length(),buf,0);
file.write(buf);
//Write the score into the file
file.writeFloat(score);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
file.close();
}
catch(Exception e)
{
System.out.println("File can't close properly!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -