Saving string to text file in Java IO

Click For Summary
SUMMARY

This discussion focuses on implementing file input/output (IO) in Java, specifically for saving and opening employee data using the Java Swing JFileChooser. The user successfully implemented the "Open" functionality to read employee data from a file, but sought clarification on how to implement the "Save" and "Save As" functionalities. The distinction between the two is clarified: "Save" updates an existing file, while "Save As" allows the user to specify a new file name or location. The user ultimately resolved their questions by understanding the necessary file handling techniques using Java's FileWriter class.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of Java Swing components, particularly JFileChooser
  • Knowledge of file handling in Java, specifically FileReader and FileWriter
  • Basic object-oriented programming concepts, including classes and methods
NEXT STEPS
  • Implement Java FileWriter for writing data to files
  • Explore Java exception handling for robust file operations
  • Learn about Java Swing event handling for user interactions
  • Study best practices for naming conventions in Java programming
USEFUL FOR

Java developers, students learning file IO operations, and anyone looking to enhance their skills in using Java Swing for GUI applications.

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 ·
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 27 ·
Replies
27
Views
24K
  • · Replies 31 ·
2
Replies
31
Views
12K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K