好的我一直在研究这个程序(参见下面的代码),我可以将输入GUI的数据保存到文本文件中。当我单击下一个按钮清除GUI文本字段以输入另一个员工时,该文件不会保存先前和当前员工的数据。我的目标是输入一名员工,然后输入另一名员工等,然后打印出与其输入一起保存的所有员工的列表。程序有什么问题???为什么不附加?
好的我有人,员工,gui和primary的课程将按照课程顺序粘贴代码。
import java.io.Serializable;
public class person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String Social, FirstName, LastName, MiddleName, Address;
public person(){
}
public person(String soc){
this(soc,null);
}
public person(String soc, String fn){
this(soc,fn,null);
}
public person(String soc,String fn, String ln){
this(soc,fn,ln,null);
}
public person(String soc,String fn,String ln,String mn){
this(soc,fn,ln,mn,null);
}
public person(String soc,String fn,String ln,String mn,String add){
setData(soc,fn, ln, mn, add);
}
public void setData(String soc,String fn,String ln,String mn,String add){
setSocial (soc);
setFirstName (fn);
setLastName (ln);
setMiddleName (mn);
setAddress (add);
}
public person(person obj)
{
Social = obj.Social;
FirstName = obj.FirstName;
MiddleName = obj.MiddleName;
LastName = obj.LastName;
Address = obj.Address;
}
public void makeCopy(person obj)
{
Social = obj.Social;
FirstName = obj.FirstName;
MiddleName = obj.MiddleName;
LastName = obj.LastName;
Address = obj.Address;
}
public person getCopy()
{
person obj = new person(this.Social);
obj.FirstName = FirstName;
obj.MiddleName = MiddleName;
obj.LastName = LastName;
obj.Address = Address;
return obj;
}
public void input(){
System.out.println("Please enter first name");
// FirstName = entry.nextLine();
}
public void input2(){
System.out.println("Please enter middle name");
// MiddleName = entry.nextLine();
}
public void input3(){
System.out.println("Please enter last name");
//LastName = entry.nextLine();
}
public void input4(){
System.out.println("Please enter social secuirty number");
// Social = entry.nextLine();
}
public void input5(){
System.out.println("Please enter your address");
// Address = entry.nextLine();
}
public void setSocial(String soc){
Social = soc;
}
public void setFirstName(String fn){
FirstName = fn;
}
public void setLastName(String ln){
LastName = ln;
}
public void setMiddleName(String mn){
MiddleName = mn;
}
public void setAddress(String add){
this.Address = add;
}
public String getSocial(){
return Social;
}
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getMiddleName(){
return MiddleName;
}
public String getAddress(){
return Address;
}
public void personData(){
String data = getFirstName()+" "+ getMiddleName()+" "+ getLastName()+ " "
+getSocial()+ " " +getAddress();
System.out.println(data);
}
}
import java.io.Serializable;
import java.util.Formatter;
public class employee extends person implements Serializable {
private static final long serialVersionUID = 1L;
private String EmployeeId;
double Rate;
double Hours;
double Gross;
private String Department;
public employee()
{
super();
EmployeeId = "0";
Rate = 0.0;
Hours = 0.0;
Gross = 0.0;
Department = "";
setSocial("");
setLastName("");
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(String id)
{
EmployeeId = id;
Rate = 0.0;
Hours = 0.0;
Gross = 0.0;
Department = "";
setSocial("");
setLastName("");
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(String id, String last)
{
EmployeeId = id;
Rate = 0.0;
Hours = 0.0;
Gross = 0.0;
setSocial("");
setLastName(last);
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(String id, String last, String Department)
{
EmployeeId = id;
Rate = 0.0;
Hours = 0.0;
Gross = 0.0;
this.Department = Department;
setSocial("");
setLastName(last);
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(String id, String last, double rate, double hours)
{
EmployeeId = id;
this.Rate = rate;
this.Hours = hours;
Gross = rate * hours;
Department = "";
setSocial("");
setLastName(last);
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(String id, String last, String Department, double rate, double
hours)
{
EmployeeId = id;
this.Rate = rate;
this.Hours = hours;
Gross = rate * hours;
this.Department = Department;
setSocial("");
setLastName(last);
setMiddleName("");
setFirstName("");
setAddress("");
}
public employee(employee obj)
{
EmployeeId = obj.EmployeeId;
Rate = obj.Rate;
Hours = obj.Hours;
Gross = obj.Gross;
Department = obj.Department;
setSocial(obj.getSocial());
setLastName(obj.getLastName());
setMiddleName(obj.getMiddleName());
setFirstName(obj.getFirstName());
setAddress(obj.getAddress());
}
public void makeCopy(employee obj)
{
EmployeeId = obj.EmployeeId;
Rate = obj.Rate;
Hours = obj.Hours;
Gross = obj.Gross;
Department = obj.Department;
setSocial(obj.getSocial());
setLastName(obj.getLastName());
setMiddleName(obj.getMiddleName());
setFirstName(obj.getFirstName());
setAddress(obj.getAddress());
}
@Override
public employee getCopy()
{
employee obj = new employee();
obj.EmployeeId = EmployeeId;
obj.Rate = Rate;
obj.Hours = Hours;
obj.Gross = Gross;
obj.Department = Department;
obj.setSocial(getSocial());
obj.setLastName(getLastName());
obj.setMiddleName(getMiddleName());
obj.setFirstName(getFirstName());
obj.setAddress(getAddress());
return obj;
}
/**
*
* @return
*/
public void setEmployeeID(String id)
{
EmployeeId = id;
}
public String getID()
{
return EmployeeId;
}
public double getRate()
{
return Rate;
}
public double getHours()
{
return Hours;
}
public double getGross()
{
return Gross;
}
public String getDepartment()
{
return Department;
}
public void setRate(double rate)
{
this.Rate = rate;
this.Gross = rate * Hours;
}
public void setHours(double hours)
{
this.Hours = hours;
this.Gross = Rate * hours;
}
public void setGross(double gross)
{
this.Gross = Rate * Hours;
}
public void setDepartment(String Department)
{
this.Department = Department;
}
public double calculateGross()
{
Gross = Rate * Hours;
return Gross;
}
public boolean equals(employee test)
{
return EmployeeId.equals(test.EmployeeId);
}
@Override
public String toString()
{
return ("employee ID: " + getID() + "\nSocial: " + getSocial() + "\nName: "
+ getLastName() + ", " + getFirstName() + " " +
getMiddleName() + "\nHome Address: " + getAddress() +
"\nDepartment: " + getDepartment() + "\nRate = " +
String.format("%,.2f, Rate = %,.2f, Gross = %,.2f\n",
getRate(), getHours(),getGross()));
}
public int IsBefore(employee obj, String sortByWhat) {
if (sortByWhat.equalsIgnoreCase("firstname"))
{
int t = this.getFirstName().compareTo(obj.getFirstName());
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
else if (sortByWhat.equalsIgnoreCase("lastname"))
{
int t = this.getLastName().compareTo(obj.getLastName());
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
else if (sortByWhat.equalsIgnoreCase("lastnamefirstname"))
{
int t = getLastName().compareTo(obj.getLastName());
if (t == 0)
{
t = getFirstName().compareTo(obj.getFirstName());
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
else if (sortByWhat.equalsIgnoreCase("rate"))
{
double t = getRate() - obj.getRate();
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
else if (sortByWhat.equalsIgnoreCase("gross"))
{
double t = getGross() - obj.getGross();
return ((t > 0) ? 1 : ((t<0) ? -1 : 0));
}
else
return 0;
}
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Formatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class gui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel idLabel;
private JLabel socLabel;
private JLabel fNameLabel;
private JLabel mNameLabel;
private JLabel lNameLabel;
private JLabel AddLabel;
private JLabel deptLabel;
private JLabel rateLabel;
private JLabel hoursLabel;
private JTextField idField;
private JTextField socField;
private JTextField fNameField;
private JTextField mNameField;
private JTextField lNameField;
private JTextField AddField;
private JTextField deptField;
private JTextField rateField;
private JTextField hoursField;
private JButton btnGross;
private JButton btnExit;
private JButton btnDelete;
private JButton btnDisplay;
private JButton btnSave;
private JButton btnNext;
private double storegross;
private thehandler handler;
private gui theApp;
double Hours, Rate;
private Formatter g;
public gui(){
super ("this is the title");
setLayout(new GridLayout(0,2));
fNameLabel= new JLabel("Please enter your first name");
add( fNameLabel );
fNameField = new JTextField(10);
add (fNameField);
mNameLabel = new JLabel("Please enter your middle name");
add( mNameLabel );
mNameField = new JTextField(10);
add (mNameField);
lNameLabel = new JLabel ("Please enter your last name");
add( lNameLabel );
lNameField = new JTextField (10);
add (lNameField);
socLabel = new JLabel("Please enter Social security number");
add(socLabel);
socField = new JTextField(10);
add (socField);
AddLabel = new JLabel("Please enter your address") ;
add(AddLabel);
AddField = new JTextField(10);
add (AddField);
deptLabel = new JLabel("Please enter your department") ;
add( deptLabel );
deptField= new JTextField(10);
add (deptField);
idLabel = new JLabel("Please enter your ID number");
add (idLabel);
idField = new JTextField(10);
add (idField);
hoursLabel = new JLabel("Please enter your hours") ;
add( hoursLabel );
hoursField= new JTextField(10);
add (hoursField);
rateLabel = new JLabel("Please enter your rate") ;
add(rateLabel );
rateField = new JTextField(10);
add (rateField);
btnGross = new JButton("Gross");
add(btnGross);
btnDisplay = new JButton("Display Employee list");
add(btnDisplay);
btnSave = new JButton("Save Employee");
add(btnSave);
btnNext = new JButton("Next");
add(btnNext);
btnExit = new JButton("Exit");
add(btnExit);
btnDelete = new JButton("Delete employee");
add(btnDelete);
thehandler handler = new thehandler();
btnGross.addActionListener(handler);
btnDisplay.addActionListener(handler);
btnSave.addActionListener(handler);
btnNext.addActionListener(handler);
btnDelete.addActionListener(handler);
btnExit.addActionListener(handler);
theApp = this;
}
public gui(employee emp){
super ("this is the title");
setLayout(new GridLayout(0,2));
fNameLabel= new JLabel("Please enter your first name");
add( fNameLabel );
fNameField = new JTextField(10);
fNameField.setText(emp.getFirstName());
add (fNameField);
mNameLabel = new JLabel("Please enter your middle name");
add( mNameLabel );
mNameField = new JTextField(emp.getMiddleName());
add (mNameField);
lNameLabel = new JLabel ("Please enter your last name");
add( lNameLabel );
lNameField = new JTextField (emp.getLastName());
add (lNameField);
socLabel = new JLabel("Please enter Social security number");
add(socLabel);
socField = new JTextField(emp.getSocial());
add (socField);
AddLabel = new JLabel("Please enter your address") ;
add(AddLabel);
AddField = new JTextField(emp.getAddress());
add (AddField);
deptLabel = new JLabel("Please enter your department") ;
add( deptLabel );
deptField= new JTextField(emp.getAddress());
add (deptField);
idLabel = new JLabel("Please enter your ID number");
add (idLabel);
idField = new JTextField(10);
add (idField);
hoursLabel = new JLabel("Please enter your hours") ;
add( hoursLabel );
hoursField= new JTextField(String.format("%7.2f", emp.getHours()));
add (hoursField);
rateLabel = new JLabel("Please enter your rate") ;
add(rateLabel );
rateField= new JTextField(String.format("%7.2f", emp.getRate()));
add (rateField);
btnGross = new JButton("Gross");
add(btnGross);
btnDisplay = new JButton("Display Employee list");
add(btnDisplay);
btnSave = new JButton("Save Employee");
add(btnSave);
btnNext = new JButton("Next");
add(btnNext);
btnExit = new JButton("Exit");
add(btnExit);
btnDelete = new JButton("Delete employee");
add(btnDelete);
thehandler handler = new thehandler();
btnGross.addActionListener(handler);
btnDisplay.addActionListener(handler);
btnSave.addActionListener(handler);
btnNext.addActionListener(handler);
btnDelete.addActionListener(handler);
btnExit.addActionListener(handler);
theApp = this;
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == btnSave)
{
employee x = new employee();
x.setSocial(socField.getText());
x.setFirstName(fNameField.getText());
x.setMiddleName(mNameField.getText());
x.setLastName(lNameField.getText());
x.setAddress(AddField.getText());
x.setDepartment(deptField.getText());
x.setEmployeeID(idField.getText());
try
{
Hours = Double.parseDouble(hoursField.getText());
Rate = Double.parseDouble(rateField.getText());
x.setHours(Hours);
x.setRate(Rate);
FileOutputStream fos = new
FileOutputStream("C:/CSCI/save.txt",true);
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(x);
oos.close();
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(null,
ex.getMessage().toString());
return;
}
catch(IOException ex1)
{
JOptionPane.showMessageDialog(null,
ex1.getMessage().toString());
return;
}
catch(Exception ex2)
{
JOptionPane.showMessageDialog(null,
ex2.getMessage().toString());
return;
}
JOptionPane.showMessageDialog(null, "Saved");
}
else if (e.getSource() == btnGross)
{
Hours = Double.parseDouble(hoursField.getText());
Rate = Double.parseDouble(rateField.getText());
storegross = (Hours * Rate);
JOptionPane.showMessageDialog(null, "The gross for
this employee is " + storegross);
}
else if (e.getSource()==btnNext)
{
try
{
idField.setText("");
socField.setText("");
fNameField.setText("");
mNameField.setText("");
lNameField.setText("");
AddField.setText("");
deptField.setText("");
rateField.setText(String.format("%7.2f", 0.0));
hoursField.setText(String.format("%7.2f", 0.0));
theApp.repaint();
}
catch (Exception ex3)
{
JOptionPane.showMessageDialog(null,
ex3.getMessage().toString());
return;
}
}
else if (e.getSource()==btnDisplay)
{
try {
FileInputStream fis = new
FileInputStream("C:/CSCI/save.txt");
ObjectInputStream ois = new
ObjectInputStream(fis);
System.out.print(ois.readObject());
ois.close();
}
catch(ClassNotFoundException ex)
{
}
catch (IOException ex)
{
}
}
else if (e.getSource() ==btnExit)
{
System.exit(0);
}
}
}
}
import java.io.*;
import javax.swing.JFrame;
public class primary {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
gui go = new gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(465,390);
go.setVisible(true);
go.setLocationRelativeTo(null);
}
}
答案 0 :(得分:1)
我在帖子中找到的唯一与IO相关的代码是
FileInputStream fis = new
FileInputStream("C:/CSCI/save.txt");
ObjectInputStream ois = new
ObjectInputStream(fis);
System.out.print(ois.readObject());
ois.close();
然而,这无法将任何内容保存到save.txt
。这将是读取内容并将其打印到控制台输出...要将字符串保存到地址,您可以使用:
public void save(String text, String address) {
FileWriter writer = null;
try {
writer = new FileWriter(address);
writer.write(text);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
if (writer != null) writer.close();
}