- #1
concon
- 65
- 0
Homework Statement
Hey everyone, I have a question regarding file IO in java.
My assignment says: Extend program 2-add menu items Open(reads employees from a file),Save(writes employees to a file already specified), and Save As(writes employees to a new or different file than the one specified.
Follow standard Windows conventions for dealing with docs and files.
Homework Equations
It's a program so no equations, but it would help to explain what the program is doing. Basically I have classes Employee,SalaryEmployee,and HourlyEmployee that have data name, age, and income. In the class EmployeeList I have an arraylist of employees and methods addData and newData which data information given from the user in an input dialog screen and add them to the list and display them in a Jpanel on the frame.
The Attempt at a Solution
Okay so I have Open done:
final JFileChooser jfc = new JFileChooser();
jmi = jm.add(new JMenuItem("Open",'O'));
jmi.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(jfc.showOpenDialog(null)== JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
int size = (int)f.length();
char[]ch = new char[size];
try{
FileReader fr = new FileReader(f);
fr.read(ch,0,size);
fr.close();
String s = new String(ch);
list.addData(s);
}
catch(FileNotFoundException ex){
System.out.println("File Does Not Exist"+ ex);
}
catch(IOException excp){
System.out.println("Error Reading File"+excp);
}
}
}
});
--and Open works totally fine. It updates the screen with the data written in data.txt
My question is how to do the save and save as functions. I'm really confused about the difference between the two as my professors instructions are kinda vague.
So far on Save I have written:
jmi = jm.add(new JMenuItem("Save",'S'));
jmi.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(jfc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
}
}
});
--But I don't know what to do inside the loop.
--I think I'm supposed write to data.txt for save
--But I have no clue how to do the creating a new file for save as or saving it to an existing file