import java.io.*;
public class Employee
{
private String name;
private String no;
...........
prvate DataInputStream file=new DataInputStream(new FileInputStream("employee"));///1
public void modify()
{
///2
}
public void insert()
{
////2
}
}1.这样定义能不能,如果不行定义的一个函数内,其他函数怎么调用file
2.如何实现对其中数据的修改
3.如何插入
public class Employee
{
private String name;
private String no;
...........
prvate DataInputStream file=new DataInputStream(new FileInputStream("employee"));///1
public void modify()
{
///2
}
public void insert()
{
////2
}
}1.这样定义能不能,如果不行定义的一个函数内,其他函数怎么调用file
2.如何实现对其中数据的修改
3.如何插入
import justin.*;import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CreateSequentialFile extends JFrame{
private ObjectOutputStream output;
private BankUI userInterface;
private JButton enterButton, openButton;
public CreateSequentialFile(){
super("Creating a sequential File of Objects");
userInterface = new BankUI(4);
getContentPane().add(userInterface, BorderLayout.CENTER);
openButton = userInterface.getDoTask1Button();
openButton.setText("Save into File...");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
}
}
);
enterButton = userInterface.getDoTask2Button();
enterButton.setText("Enter");
enterButton.setEnabled(false);
enterButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
addRecord();
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent event){
if(output != null)
addRecord();
closeFile();
}
}
);
setSize(300, 200);
setVisible(true);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try{
output = new ObjectOutputStream(new FileOutputStream(fileName));
openButton.setEnabled(false);
enterButton.setEnabled(true);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile(){
try{
output.close();
System.exit(0);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error Closing File", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private void addRecord(){
int accountNumber = 0;
AccountRecord record;
String fieldValues[] = userInterface.getFieldValues();
if(!fieldValues[BankUI.ACCOUNT].equals("")){
try{
accountNumber = Integer.parseInt(fieldValues[BankUI.ACCOUNT]);
if (accountNumber > 0){
record = new AccountRecord(accountNumber, fieldValues[BankUI.FIRSTNAME], fieldValues[BankUI.LASTNAME], Double.parseDouble(fieldValues[BankUI.BALANCE]));
output.writeObject(record);
output.flush();
}
else{
JOptionPane.showMessageDialog(this, "Account number must be greater than 0", "Bad account number", JOptionPane.ERROR_MESSAGE);
}
userInterface.clearFields();
}
catch(NumberFormatException formatException){
JOptionPane.showMessageDialog(this, "Bad account number or balance", "Invalid Number Format", JOptionPane.ERROR_MESSAGE);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error writing to file", "IO Exception", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String args[]){
new CreateSequentialFile();
}
}import justin.*;import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ReadSequentialFile extends JFrame{
private ObjectInputStream input;
private BankUI userInterface;
private JButton nextButton, openButton;
public ReadSequentialFile(){
super("Reading a Sequential File of Objects");
userInterface = new BankUI(4);
Container container = getContentPane();
container.add(userInterface, BorderLayout.CENTER);
openButton = userInterface.getDoTask1Button();
openButton.setText("Open File");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent actionEvent){
openFile();
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent event){
if(input != null)
closeFile();
System.exit(0);
}
}
);
nextButton = userInterface.getDoTask2Button();
nextButton.setText("Next Record");
nextButton.setEnabled(false);
nextButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
readRecord();
}
}
);
pack();
setSize(300, 200);
setVisible(true);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
if(fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try{
input = new ObjectInputStream(new FileInputStream(fileName));
openButton.setEnabled(false);
nextButton.setEnabled(true);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void readRecord(){
AccountRecord record;
try{
record = (AccountRecord) input.readObject();
String[] values = {String.valueOf(record.getAccount()), record.getFirstName(), record.getLastName(), String.valueOf(record.getBalance())};
userInterface.setFieldValues(values);
}
catch(EOFException endOfFileException){
nextButton.setEnabled(false);
}
catch(ClassNotFoundException classNotFoundException){
JOptionPane.showMessageDialog(this, "Unable to create object", "Class Not Found", JOptionPane.ERROR_MESSAGE);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error during read from file", "Read Error", JOptionPane.ERROR_MESSAGE);
}
}
private void closeFile(){
try{
input.close();
System.exit(0);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this, "Error closing file", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[]){
new ReadSequentialFile();
}
}
现在又遇到一问题,怎么从actionperformed中把数据传出来
我觉得,按我的理解,楼主,是想将多个雇员信息保存到文件当中,当然,保存的文件内容要与实际内存中的数据要保持同步。那么,实际上楼主可以设计三个类,这样思路会比较清晰。第一个类,实现存放每个雇员的信息,名称可以叫做Employee,成员变量可以有姓名,年龄,编号等等,但是,要实现Serializable接口(就是那个序列化接口)
第二个类,实现存放多个雇员的集合对象,可以是LinkedList,或者其他的集合类(聚集类)也行。
第三个类,完成对雇员集合对象,与文件对象的数据同步。实际上,楼主写两个类就可以了。
原来的Employee代码不用改动,只把里面的带注释的相关语句写到另一个类里就可以了。
我随便写写代码,给楼主个启发,因为在网吧上网,没办法检查语法规则以及编译调试,只能凑合看思路了。
/**该类存放每个雇员的信息,当然,要有相应的Setter和Getter**/
public class Employee {
private String name;
private String no;
//...........
} /** 这个类完成雇员集合的插入,更改,以及文件信息的同步操作 **/
public class EmployeeCollection{
private List employeeList = new LinkedList();
private String fileName;
public EmployeeCollection(String fileName){
this.fileName=fileName;
}
public void modify(Employee oldEmp,Employee newEmp) {
employeeList.remove(oldEmp);
employeeList.add(newEmp);
}
public void insert(Employee emp){
employeeList.add(emp);
}
public void saveToFile()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName,false));
oos.writeObject(employeeList);
oos.close();
}
public void loadFromFile()throws IOException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filName));
employeeList = (List)ois.readObject();
ois.close();
}
}如果要自动完成文件同步操作,还可以写一个自动完成同步操作的类,但是,有可能会使性能降低。
示例代码如下:
/** 通过继承EmployeeCollection类,重写其中的一些方法,完成功能的扩展。(装饰模式) **/
public class EmployeeCollectionWraper extends EmployeeCollection{
private EmployeeCollection ec;
public EmployeeCollectionWraper(EmployeeCollection ec){
this.ec = ec;
}
public void modify(Employee oldEmp,Employee newEmp)throws IOException{
super.modify(oldEmp,newEmp);
super.saveToFile();
}
public void insert(Employee emp)throws IOException{
super.insert(emp);
super.saveToFile();
} 以上代码近供参考,楼主可根据实际情况进行增改。
谢谢。
不好意思。
在具体使用的时候,楼主主要针对EmployeeCollection 或者EmployeeCollectionWraper 类来进行操作就可以了,
应用起来,应该是比较方便的。
哈哈。
/**该类存放每个雇员的信息,当然,要有相应的Setter和Getter**/
public class Employee implements Serializable{
private String name;
private String no;
//...........
} /** 这个类完成雇员集合的插入,更改,以及文件信息的同步操作 **/
public class EmployeeCollection{
private List employeeList = new LinkedList();
private String fileName;
public EmployeeCollection(String fileName){
this.fileName=fileName;
}
public void modify(Employee oldEmp,Employee newEmp) {
employeeList.remove(oldEmp);
employeeList.add(newEmp);
}
public void insert(Employee emp){
employeeList.add(emp);
}
public void saveToFile()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName,false));
oos.writeObject(employeeList);
oos.close();
}
public void loadFromFile()throws IOException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filName));
employeeList = (List)ois.readObject();
ois.close();
}
}/** 通过继承EmployeeCollection类,重写其中的一些方法,完成功能的扩展。(装饰模式) **/
public class EmployeeCollectionWraper extends EmployeeCollection{
private EmployeeCollection ec;
public EmployeeCollectionWraper(EmployeeCollection ec){
this.ec = ec;
ec.loadFromFile();
}
public void modify(Employee oldEmp,Employee newEmp)throws IOException{
ec.modify(oldEmp,newEmp);
ec.saveToFile();
}
public void insert(Employee emp)throws IOException{
ec.insert(emp);
ec.saveToFile();
} 至于怎么使用,我这里就不用说了吧?
如果你要手动的进行与文件信息的同步操作,那么,new一个EmployeeCollection类的对象,就应该可以了。
如果你要实现对文件的自动同步操作,那么,还要再new一个EmployeeCollectionWraper类的对象。
当然,要注意,当你更改某个Employee对象的信息的时候,实际上,EmployeeCollection在内存上所保存的那个Employee对象也会跟着改变,
这时,是要楼主手动调用EmployeeCollection.saveToFile()方法来完成信息的同步的。
还有,上述的modify方法,当初我给理解成用新的对象替换老的对象了,如果只是某个对象的自身内容进行更改,就没有必要那么复杂了。
示例代码如下:
/** 这个类完成雇员集合的插入,更改,以及文件信息的同步操作 **/
public class EmployeeCollection{
private List employeeList = new LinkedList();
private String fileName;
public EmployeeCollection(String fileName){
this.fileName=fileName;
}
public void modify(Employee emp) {
return;//什么也不做,因为employeeList保存的只是对象的引用。
}
public void insert(Employee emp){
employeeList.add(emp);
}
public void saveToFile()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName,false));
oos.writeObject(employeeList);
oos.close();
}
public void loadFromFile()throws IOException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filName));
employeeList = (List)ois.readObject();
ois.close();
}
}/** 通过继承EmployeeCollection类,重写其中的一些方法,完成功能的扩展。(装饰模式) **/
public class EmployeeCollectionWraper extends EmployeeCollection{
private EmployeeCollection ec;
public EmployeeCollectionWraper(EmployeeCollection ec){
this.ec = ec;
ec.loadFromFile();
}
public void modify(Employee emp)throws IOException{
ec.modify(emp);//这条语句,可有可无。下面那条必须要有。
ec.saveToFile();
}
public void insert(Employee emp)throws IOException{
ec.insert(emp);
ec.saveToFile();
}
/**该类存放每个雇员的信息,当然,要有相应的Setter和Getter**/
public class Employee implements Serializable{
private String name;
private String no;
//...........
} /** 这个类完成雇员集合的插入,更改,以及文件信息的同步操作 **/
public class EmployeeCollection{
private List employeeList = new LinkedList();
private String fileName;
public EmployeeCollection(String fileName){
this.fileName=fileName;
}
public void modify(Employee oldEmp,Employee newEmp) {
employeeList.remove(oldEmp);
employeeList.add(newEmp);
}
public void insert(Employee emp){
employeeList.add(emp);
}
public void saveToFile()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName,false));
oos.writeObject(employeeList);
oos.close();
}
public void loadFromFile()throws IOException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filName));
employeeList = (List)ois.readObject();
ois.close();
}
}/** 通过继承EmployeeCollection类,重写其中的一些方法,完成功能的扩展。(装饰模式) **/
public class EmployeeCollectionWraper extends EmployeeCollection{
private EmployeeCollection ec;
public EmployeeCollectionWraper(EmployeeCollection ec){
this.ec = ec;
ec.loadFromFile();
}
public void modify(Employee oldEmp,Employee newEmp)throws IOException{
ec.modify(oldEmp,newEmp);
ec.saveToFile();
}
public void insert(Employee emp)throws IOException{
ec.insert(emp);
ec.saveToFile();
} 至于怎么使用,我这里就不用说了吧?
如果你要手动的进行与文件信息的同步操作,那么,new一个EmployeeCollection类的对象,就应该可以了。
如果你要实现对文件的自动同步操作,那么,还要再new一个EmployeeCollectionWraper类的对象。
当然,要注意,当你更改某个Employee对象的信息的时候,实际上,EmployeeCollection在内存上所保存的那个Employee对象也会跟着改变,
这时,是要楼主手动调用EmployeeCollection.saveToFile()方法来完成信息的同步的。
还有,上述的modify方法,当初我给理解成用新的对象替换老的对象了,如果只是某个对象的自身内容进行更改,就没有必要那么复杂了。
示例代码如下:
/** 这个类完成雇员集合的插入,更改,以及文件信息的同步操作 **/
public class EmployeeCollection{
private List employeeList = new LinkedList();
private String fileName;
public EmployeeCollection(String fileName){
this.fileName=fileName;
}
public void modify(Employee emp) {
return;//什么也不做,因为employeeList保存的只是对象的引用。
}
public void insert(Employee emp){
employeeList.add(emp);
}
public void saveToFile()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName,false));
oos.writeObject(employeeList);
oos.close();
}
public void loadFromFile()throws IOException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filName));
employeeList = (List)ois.readObject();
ois.close();
}
}/** 通过继承EmployeeCollection类,重写其中的一些方法,完成功能的扩展。(装饰模式) **/
public class EmployeeCollectionWraper extends EmployeeCollection{
private EmployeeCollection ec;
public EmployeeCollectionWraper(EmployeeCollection ec){
this.ec = ec;
ec.loadFromFile();
}
public void modify(Employee emp)throws IOException{
ec.modify(emp);//这条语句,可有可无。下面那条必须要有。
ec.saveToFile();
}
public void insert(Employee emp)throws IOException{
ec.insert(emp);
ec.saveToFile();
}