在随机访问文件中添加数据:java gui
我正在尝试创建一个学生信息系统,可以添加、更新、查看、删除和搜索数据。我正在编写添加数据的代码,请告诉我哪里出了问题,因为每次输入roll no时,它只接受第一个,然后提示“roll no ready exists”。但是如果我关闭jar文件并再次打开它,然后输入roll no,它将第一次接受。我已经创建了一个.dat文件来输入数据 另外,我是一名初学者,刚刚开始学习java在随机访问文件中添加数据:java gui,java,file,user-interface,random-access,Java,File,User Interface,Random Access,我正在尝试创建一个学生信息系统,可以添加、更新、查看、删除和搜索数据。我正在编写添加数据的代码,请告诉我哪里出了问题,因为每次输入roll no时,它只接受第一个,然后提示“roll no ready exists”。但是如果我关闭jar文件并再次打开它,然后输入roll no,它将第一次接受。我已经创建了一个.dat文件来输入数据 另外,我是一名初学者,刚刚开始学习java import java.io.*; public class Record { int rollNo, standa
import java.io.*;
public class Record
{
int rollNo, standard;
String firstName, lastName, address;
public void read( RandomAccessFile file) throws IOException
{
rollNo = file.readInt();
byte b1[] = new byte[15];
file.readFully(b1);
firstName = new String(b1, 0);
byte b2[] = new byte[15];
file.readFully(b2);
lastName = new String(b2, 0);
standard = file.readInt();
byte b3[] = new byte[15];
file.readFully(b3);
address = new String(b3, 0);
}
public void write( RandomAccessFile file) throws IOException
{
file.writeInt( rollNo );
byte b1[] = new byte[15];
if( firstName != null)
firstName.getBytes( 0 , firstName.length(), b1, 0);
file.write(b1);
byte b2[] = new byte[15];
if( lastName != null)
lastName.getBytes( 0, lastName.length(), b2, 0);
file.write(b2);
file.writeInt( standard );
byte b3[] = new byte[15];
if( lastName != null)
address.getBytes( 0, address.length(), b3, 0);
file.write(b3);
}
public int size(){ return 53;}
}
import java.io.*;
import java.awt.*;
public class StudentInformation extends Frame
{
Button updateButton, newButton, deleteButton, viewButton, done;
UpdateRec update;
NewRec newRec;
DeleteRec deleteRec;
ViewRec viewRec;
RandomAccessFile file;
Record data;
public StudentInformation()
{
super("Student Database");
try
{
file = new RandomAccessFile("credit.dat", "rw");
}
catch(IOException e)
{
System.err.println(e.toString());
System.exit(1);
}
data = new Record();
setup();
}
public void setup()
{
resize(300, 120);
setLayout(new GridLayout(3,2));
updateButton = new Button("Update Record");
newButton = new Button("New Record");
deleteButton = new Button("Delete Record");
viewButton = new Button("View Records");
done = new Button("Done");
add(updateButton);
add(newButton);
add(deleteButton);
add(viewButton);
add(done);
show();
update = new UpdateRec(file);
newRec = new NewRec(file);
deleteRec = new DeleteRec(file);
viewRec = new ViewRec(file);
}
public boolean action(Event event, Object o)
{
if(event.target instanceof Button)
{
String current = (String)event.arg;
if(current.equals("Update Record"))
update.show();
else if(current.equals("New Record"))
newRec.show();
else if(current.equals("Delete Record"))
deleteRec.show();
else if(current.equals("View Records"))
viewRec.show();
}
return true;
}
public boolean handleEvent(Event event)
{
if(event.id == Event.WINDOW_DESTROY || event.target == done)
{
cleanup();
hide();
dispose();
System.exit(0);
return true;
}
return super.handleEvent(event);
}
public void cleanup()
{
try
{
file.close();
}
catch(IOException e)
{
System.err.println(e.toString());
System.exit(1);
}
}
public static void main(String args[])
{
StudentInformation teller = new StudentInformation();
}
}
class NewRec extends Dialog
{
RandomAccessFile file;
TextField roll, fname, lname, stnd, addr;
Button save, cancel;
Label rollLabel, fnameLabel, lnameLabel, stndLabel, addLabel;
Record data;
int rollNo;
public NewRec(RandomAccessFile f)
{
super(new Frame(), "New Record", true);
resize(300,150);
setLayout(new GridLayout(6,2));
file=f;
roll = new TextField(20);
rollLabel = new Label("rollNo");
fname = new TextField(20);
fnameLabel = new Label("First Name");
lname = new TextField(20);
lnameLabel = new Label("Last Name");
stnd = new TextField(20);
stndLabel = new Label("Class");
addr = new TextField(20);
addLabel = new Label("Address");
save = new Button("Save Changes");
cancel = new Button("Cancel");
add(rollLabel);
add(roll);
add(fnameLabel);
add(fname);
add(lnameLabel);
add(lname);
add(stndLabel);
add(stnd);
add(addLabel);
add(addr);
add(save);
add(cancel);
data = new Record();
}
public boolean action(Event event, Object o)
{
if( event.target == save)
{
rollNo = Integer.parseInt(roll.getText());
if(rollNo<1)
{
roll.setText("Invalid Roll Num");
return true;
}
try
{
file.seek((rollNo-1)*data.size());
data.read(file);
}
catch( IOException e)
{
}
if(data.rollNo!=0)
{
roll.setText(String.valueOf(data.rollNo) + " already exists");
fname.setText("");
lname.setText("");
stnd.setText("");
addr.setText("");
}
if(data.rollNo==0)
{
try
{
data.rollNo = rollNo;
data.lastName = lname.getText();
data.firstName = fname.getText();
data.standard = Integer.parseInt(stnd.getText());
data.address = addr.getText();
file.seek((rollNo-1)*data.size());
data.write(file);
}
catch( IOException e)
{
roll.setText("Error Writing File" + e.toString());
return true;
}
hide();
clear();
}
}
else if(event.target == cancel)
{
hide();
clear();
}
return true;
}
private void clear()
{
roll.setText("");
fname.setText("");
lname.setText("");
stnd.setText("");
addr.setText("");
}
}
import java.io.*;
公开课记录
{
int-rollNo,标准;
字符串firstName、lastName、address;
公共无效读取(随机访问文件)引发IOException
{
rollNo=file.readInt();
字节b1[]=新字节[15];
文件.readFully(b1);
firstName=新字符串(b1,0);
字节b2[]=新字节[15];
文件。可读(b2);
lastName=新字符串(b2,0);
标准=file.readInt();
字节b3[]=新字节[15];
文件。可读(b3);
地址=新字符串(b3,0);
}
公共无效写入(随机访问文件)引发IOException
{
file.writeInt(rollNo);
字节b1[]=新字节[15];
if(firstName!=null)
getBytes(0,firstName.length(),b1,0);
文件写入(b1);
字节b2[]=新字节[15];
if(lastName!=null)
getBytes(0,lastName.length(),b2,0);
文件写入(b2);
文件写入(标准);
字节b3[]=新字节[15];
if(lastName!=null)
address.getBytes(0,address.length(),b3,0);
文件写入(b3);
}
public int size(){return 53;}
}
导入java.io.*;
导入java.awt.*;
公共班级学生信息扩展框架
{
按钮更新按钮、新按钮、删除按钮、查看按钮、完成;
更新,更新;
NewRec NewRec;
DeleteRec DeleteRec;
ViewRec ViewRec;
随机存取文件;
记录数据;
公共学生信息()
{
超级(“学生数据库”);
尝试
{
文件=新的随机访问文件(“credit.dat”、“rw”);
}
捕获(IOE异常)
{
System.err.println(例如toString());
系统出口(1);
}
数据=新记录();
设置();
}
公共作废设置()
{
调整大小(300120);
setLayout(新网格布局(3,2));
updateButton=新按钮(“更新记录”);
新按钮=新按钮(“新记录”);
deleteButton=新按钮(“删除记录”);
viewButton=新按钮(“查看记录”);
完成=新按钮(“完成”);
添加(更新按钮);
添加(新按钮);
添加(删除按钮);
添加(查看按钮);
添加(完成);
show();
update=新的UpdateRec(文件);
newRec=newnewrec(文件);
deleteRec=新的deleteRec(文件);
viewRec=新viewRec(文件);
}
公共布尔操作(事件,对象o)
{
if(event.target instanceof按钮)
{
字符串当前=(字符串)event.arg;
if(当前等于(“更新记录”))
update.show();
else if(当前等于(“新记录”))
newRec.show();
else if(当前.equals(“删除记录”))
deleteRec.show();
else if(当前.equals(“查看记录”))
view rec.show();
}
返回true;
}
公共布尔handleEvent(事件)
{
if(event.id==event.WINDOW_DESTROY | | event.target==done)
{
清理();
隐藏();
处置();
系统出口(0);
返回true;
}
返回super.handleEvent(事件);
}
公共空间清理()
{
尝试
{
file.close();
}
捕获(IOE异常)
{
System.err.println(例如toString());
系统出口(1);
}
}
公共静态void main(字符串参数[])
{
StudentInformation teller=新的StudentInformation();
}
}
类NewRec扩展对话框
{
随机存取文件;
TextField roll、fname、lname、stnd、addr;
按钮保存,取消;
标签rollLabel、fnameLabel、lnameLabel、stndLabel、addLabel;
记录数据;
int-rollNo;
公共NewRec(随机访问文件f)
{
super(新帧(),“新记录”,true);
调整大小(300150);
setLayout(新网格布局(6,2));
file=f;
滚动=新文本字段(20);
rollLabel=新标签(“rollNo”);
fname=新文本字段(20);
fnameLabel=新标签(“名字”);
lname=新文本字段(20);
lnameLabel=新标签(“姓氏”);
stnd=新文本字段(20);
stndLabel=新标签(“类别”);
addr=新文本字段(20);
addLabel=新标签(“地址”);
保存=新建按钮(“保存更改”);
取消=新按钮(“取消”);
添加(滚动标签);
添加(滚动);
添加(fnameLabel);
添加(fname);
添加(lnameLabel);
添加(lname);
添加(stndLabel);
添加(stnd);
添加(添加标签);
添加(addr);
添加(保存);
添加(取消);
数据=新记录();
}
公共布尔操作(事件,对象o)
{
如果(event.target==保存)
{
rollNo=Integer.parseInt(roll.getText());
如果(rollNo我会尽力帮助你
第一:这是一个代码检查。您应该使用:
第二:设计问题:
- 您的代码应该使用java的功能:序列化和反序列化:
- 在一个文件中随机读写,一个字节一个字节是可能的,但这是老办法,不太可靠
代码中不起作用的内容:
- 最重要的是,您不会重置数据(在写入数据之后,输入之前)
- 发生的情况取决于文件的大小,以及您之前输入的数据:如果您输入了数据15,您的文件将从偏移量0到13为空。然后,如果您尝试获取10,例如:可以,因为您获取了0。但是如果您尝试获取25,则会出现异常(超出文件的限制),然后您保留先例数据,然后您的rollNo“已存在”
基本解决方案:在每次输入前清除数据
我添加了一个try-catch,并警告错误数据
因此,我提出了一些意见,你应该考虑一下
瞧:
public boolean action(Event event, Object o)
{
if( event.target == save)
{
// SOLUTION IS HERE
data=new Record();
// TRACE
// PROBLEM HERE: data keeps precedent value
System.out.println("SAME PLAYER PLAY AGAIN: data.rollNo ACTUALLY:"+data.rollNo);
rollNo = Integer.parseInt(roll.getText());
// TRACE
System.out.println("rollNo ACTUALLY:"+rollNo);
if(rollNo<1)
{
roll.setText("Invalid Roll Num");
return true;
}
try
{
file.seek((rollNo-1)*data.size());
data.read(file);
}
catch( IOException e)
{
// TRACE
System.out.println("EXCEPTION IN FILE !");
// PROBLEM HERE: data have not been defined: then keeps precedent value
}
// TRACE
System.out.println("data.rollNo ACTUALLY:"+data.rollNo);
if(data.rollNo!=0)
{
// TRACE
System.out.println("ALREADY EXISTS");
roll.setText(String.valueOf(data.rollNo) + " already exists");
fname.setText("");
lname.setText("");
stnd.setText("");
addr.setText("");
}
if(data.rollNo==0)
{
// TRACE
System.out.println("NOT FOUND");
try
{
try
{
data.rollNo = rollNo;
data.lastName = lname.getText();
data.firstName = fname.getText();
data.standard = Integer.parseInt(stnd.getText());
data.address = addr.getText();
file.seek((rollNo-1)*data.size());
data.write(file);
// TRACE
System.out.println("WRITE DATA "+rollNo);
}
catch( IOException e)
{
roll.setText("Error Writing File" + e.toString());
return true;
}
}
// SOLUTION
// WARNING: IF bad datas,
catch( Exception ex)
{
System.err.println("BAD DATAS");
JOptionPane.showMessageDialog(new JFrame(), "WARNING: all fields must be filled !", "WARNING",
JOptionPane.ERROR_MESSAGE);
return true;
}
hide();
// TRACE
System.out.println("CLEAR");
clear();
// PROBLEM HERE: data not reset ! see above
}
}
else if(event.target == cancel)
{
hide();
clear();
}
return true;
}
pu