import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;
public class First extends Frame{
static ObjectInputStream dis = null;
static ObjectOutputStream dos = null;
static FileOutputStream fos = null;
static FileInputStream fis = null;
static Hashtable<String, Student> hash = null;
static File f = null;
String s = "D:\\学生信息管理系统\\学生信息.txt";
Button b1 = new Button("登陆");
Label l0 = new Label("欢迎登陆学生管理系统");
Label l1 = new Label("用户名:");
Label l = new Label();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField t1 = new TextField("admin",9);
TextField t2 = new TextField(9);
Label l2 = new Label("密码:");
Button b2 = new Button("退出");
Second second;
public void lunchFrame(){
this.setTitle("欢迎登陆学生管理系统");
this.setLocation(100, 200);
this.setLayout(new GridLayout(5,1));
this.setBackground(Color.BLUE);
this.add(l0);
this.add(p1);
this.add(p2);
this.add(l);
l.setText("提示:如果您首次登陆用户名为 admin 密码为空!");
this.add(p3);
p1.add(l1);
p1.add(t1);
p2.add(l2);
p2.add(t2);
p3.add(b1);
p3.add(b2);
hash = new Hashtable<String, Student>();
f = new File(s);
try {
fos = new FileOutputStream(s);
dos = new ObjectOutputStream(fos);
fis = new FileInputStream(s);
dis = new ObjectInputStream(fis);
if(!f.exists()){
dos.writeObject(hash);
dos.flush();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
this.pack();
t2.setEchoChar('*');
this.setResizable(false);
this.setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
hash = (Hashtable<String, Student>) dis.readObject();
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
if(t1.getText().length() == 0){
l.setText("用户名不能为空");
}else if(t1.getText().trim().equals("admin")){
if(t2.getText().trim().length() != 0){
l.setText("初始不能有密码");
}else{
second = new Second();
second.lunchFrame();
}
}else if(t1.getText().trim().matches("[0-9]{12}")){
if(t2.getText().length() == 0){
l.setText("密码不能为空");
}else if(hash != null){
if(hash.containsKey(t1.getText().trim())){
Student student = hash.get(t2.getText());
if(student.getID().substring(11, 17).equals(t2.getText().trim())){
second = new Second();
second.lunchFrame();
}else{
l.setText("密码错误");
}
}else{
l.setText("用户名不存在");
}
}
}else{
l.setText("用户名错误");
}
try {
dos.writeObject(hash);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
dis.close();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
};
System.exit(0);
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
Frame f = (Frame)e.getSource();
f.setVisible(false);
}
});
}
public static void main(String[] args) {
new First().lunchFrame();
}
}
点击登陆就报错为什么?困扰了一下午了?求大神解答
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at First$1.actionPerformed(First.java:84)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
------解决思路----------------------
fos = new FileOutputStream(s);
dos = new ObjectOutputStream(fos);
fis = new FileInputStream(s);
dis = new ObjectInputStream(fis);
你这个四行的目的是什么呢?
从一个文件建了output stream,又建了input stream,是想又读又写么?这个肯定会有问题的。
你是不是想用管道?那应该用PipedInput/outpout