Saving string to text file in Java IO

In summary, the conversation is about a programming assignment to create a java program with file IO functionality. The program has classes for Employee, SalaryEmployee, and HourlyEmployee with data fields for name, age, and income. The main class, EmployeeList, has an arraylist of employees and methods for adding and displaying data. The main question is how to implement the Save and Save As functions, with the main difference being that Save updates an existing file while Save As allows for creating a new file. The conversation also includes tips for using FileWriter and avoiding single letter variable names.
  • #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
 
Physics news on Phys.org
  • #2
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.
 
  • #3
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.
 

What is the purpose of saving string to text file in Java IO?

The purpose of saving string to text file in Java IO is to store data in a text format that can be easily read and manipulated by both humans and computers. This is useful for saving and sharing information, as well as for data analysis and processing.

How do I save a string to a text file in Java IO?

To save a string to a text file in Java IO, you can use the FileWriter class to create a new text file and write the string to it. Make sure to handle any exceptions that may occur during the process.

Can I append a string to an existing text file in Java IO?

Yes, you can use the FileWriter class with the append flag set to true to add a string to the end of an existing text file. This allows you to continually add new data to a file without overwriting the existing content.

How do I read the contents of a text file in Java IO?

To read the contents of a text file in Java IO, you can use the FileReader class to open the file and read its contents. Alternatively, you can use the Scanner class to read the file line by line or use the BufferedReader class for more efficient reading of larger files.

Is it possible to save special characters and formatting in a text file using Java IO?

Yes, it is possible to save special characters and formatting in a text file using Java IO. You can use the PrintWriter class to format and write the data to the file, allowing you to save special characters, new lines, and other formatting options.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
27
Views
23K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top