Comp Sci Saving string to text file in Java IO

AI Thread Summary
The discussion focuses on implementing file I/O in a Java program that manages employee data. The user successfully completed the "Open" function, which reads employee data from a file and displays it. They seek clarification on how to implement the "Save" and "Save As" functions, noting confusion over their differences. The distinction is explained: "Save" updates an existing file, while "Save As" allows the user to specify a new file name or location. The user ultimately resolved their issue, realizing the importance of careful data management and variable naming conventions in programming.
concon
Messages
64
Reaction score
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
 
Physics news on Phys.org
Take a look at http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
Basically you'll be doing the opposite of what you did read.

Generally the difference between save and save as. Save you update the file to reflect the new contents where save as you'll ask for a new file name and then save all the file contents to that new file name.

Also as a general programming note.

concon said:
File f = jfc.getSelectedFile();

NEVER EVER EVER EVER EVER use single letter variable names. :) Something better would the theFile or fileHandler.
 
cpscdave said:
Take a look at http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
Basically you'll be doing the opposite of what you did read.

Generally the difference between save and save as. Save you update the file to reflect the new contents where save as you'll ask for a new file name and then save all the file contents to that new file name.

Also as a general programming note.



NEVER EVER EVER EVER EVER use single letter variable names. :) Something better would the theFile or fileHandler.

Thanks, I already figured the solution out yesterday. Of course I always use better variable names, but the reason I was even posting a question on here is I lost part of my program and had to rewrite most of it and forgot some of the specifics for file IO. I guess I learned my lesson to never save things on computers at school without triple checking that its going to my usb! I don't have my flash drive with me to copy and paste my code, but what I did was for save as you can select a location to save the output or you can create a new file. Save let's you pick an existing file to save the data to OR if a file is already open it saves it there.
 

Similar threads

Replies
6
Views
3K
Replies
10
Views
11K
Replies
1
Views
2K
Replies
31
Views
12K
Replies
3
Views
3K
Replies
3
Views
4K
Back
Top