题外话:楼主说的abc.abc文件不是真正的abc.abc文件吧,应该是stu.txt或之类的正常后缀的文件吧,呵呵
*************************************************************
我给你写了个InputInfo.java类(包括一个内部类Student),可以实现录入学生基本信息(姓名、性别、年龄和专业等)、判断信息正误(如年龄必须为整数等)和保存信息为文本等功能,代码如下:
*************************************************************
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
/**
* @author Godwin
* @version 2010-05-15
*/
public class InputInfo extends JFrame implements ActionListener {
ArrayList<Student> stus = new ArrayList<Student>();
JLabel infoLabel;
JLabel nameLabel;
JTextField name;
JLabel genderLabel;
JTextField gender;
JLabel ageLabel;
JTextField age;
JLabel majorLabel;
JTextField major;
JButton addButton;
JButton resetButton;
JButton saveasButton;
public InputInfo() {
this.setTitle("录入学生信息");
// 信息提示
infoLabel = new JLabel();
setInfo();
JPanel infoPanel = new JPanel(new FlowLayout());
infoPanel.add(infoLabel);
// 录入框
// 姓名
nameLabel = new JLabel("姓名(*):");
name = new JTextField(20);
JPanel namePanel = new JPanel(new FlowLayout());
namePanel.add(nameLabel);
namePanel.add(name);
// 性别
genderLabel = new JLabel("性别(*):");
gender = new JTextField(20);
JPanel genderPanel = new JPanel(new FlowLayout());
genderPanel.add(genderLabel);
genderPanel.add(gender);
// 年龄
ageLabel = new JLabel("年龄(*):");
age = new JTextField(20);
JPanel agePanel = new JPanel(new FlowLayout());
agePanel.add(ageLabel);
agePanel.add(age);
// 专业
majorLabel = new JLabel("专业(*):");
major = new JTextField(20);
JPanel majorPanel = new JPanel(new FlowLayout());
majorPanel.add(majorLabel);
majorPanel.add(major);
// 添加录入面板
JPanel inputPanel = new JPanel(new GridLayout(4, 1, 2, 2));
inputPanel.setBorder(BorderFactory.createTitledBorder("学生基本信息"));
inputPanel.add(namePanel);
inputPanel.add(genderPanel);
inputPanel.add(agePanel);
inputPanel.add(majorPanel);
// 按钮
addButton = new JButton("添加");
resetButton = new JButton("重置");
saveasButton = new JButton("另存为");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(resetButton);
buttonPanel.add(saveasButton);
// 添加
this.add(infoPanel, BorderLayout.NORTH);
this.add(inputPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addButton.addActionListener(this);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name.setText("");
gender.setText("");
age.setText("");
major.setText("");
name.requestFocus();
}
});
saveasButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
outputText();
}
});
}
// 添加学生信息监听
public void actionPerformed(ActionEvent e) {
if (name.getText().equals("") || name.getText() == null) {
JOptionPane.showMessageDialog(this, "请输入姓名!", "提示信息",
JOptionPane.WARNING_MESSAGE);
name.requestFocus();
return;
} else if (gender.getText().equals("") || gender.getText() == null) {
JOptionPane.showMessageDialog(this, "请输入性别!", "提示信息",
JOptionPane.WARNING_MESSAGE);
gender.requestFocus();
return;
} else if (age.getText().equals("") || age.getText() == null) {
JOptionPane.showMessageDialog(this, "请输入年龄!", "提示信息",
JOptionPane.WARNING_MESSAGE);
age.requestFocus();
return;
} else if (!(age.getText().matches("[\\d]{1,2}"))) {
JOptionPane.showMessageDialog(this, "输入有误,年龄必须为1-2位整数,请重新输入!",
"错误提示", JOptionPane.WARNING_MESSAGE);
age.requestFocus();
age.selectAll();
return;
} else if (major.getText().equals("") || major.getText() == null) {
JOptionPane.showMessageDialog(this, "请输入专业!", "提示信息",
JOptionPane.WARNING_MESSAGE);
major.requestFocus();
return;
}
stus.add(new Student(name.getText(), gender.getText(), Integer
.valueOf(age.getText()), major.getText()));
setInfo();
JOptionPane.showMessageDialog(this, "成功添加一条学生信息!");
resetButton.doClick();
}
public void setInfo() {
infoLabel.setText("现有学生数为:" + stus.size());
}
// 保存文件
public void outputText() {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
String fileName = file.getName();
if (fileName != null && fileName.trim().length() > 0) {
if (!(fileName.endsWith(".txt"))) {
fileName = fileName.concat(".txt");
}
}
file = new File(file.getParent().concat(File.separator)
.concat(fileName));
if (file.exists()) {
int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
if (i != JOptionPane.YES_OPTION) {
return;
}
}
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
StringBuffer sb = new StringBuffer();
sb.append("姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "专业\n");
for (int i = 0; i < stus.size(); i++) {
sb.append(stus.get(i) + "\n");
}
fw.write(sb.toString().trim());
fw.flush();
fw.close();
JOptionPane.showMessageDialog(this, "保存成功!", "提示信息",
JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",
JOptionPane.WARNING_MESSAGE);
return;
}
}
class Student {
String name;
String gender;
Integer age;
String major;
public Student() {
}
public Student(String name, String gender, Integer age, String major) {
this.name = name;
this.gender = gender;
this.age = age;
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String toString() {
return "" + name + "\t" + gender + "\t" + age + "\t" + major;
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new InputInfo();
}
}
*************************************************************
运行结果如下:
参考资料:还有其他问题的话,给我发百度消息
本回答被提问者采纳