1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Saving string to text file in Java IO

  1. Jul 20, 2014 #1
    1. The problem statement, all variables and given/known data
    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.





    2. Relevant 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.

    3. 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
     
  2. jcsd
  3. Jul 21, 2014 #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.

    NEVER EVER EVER EVER EVER use single letter variable names. :) Something better would the theFile or fileHandler.
     
  4. Jul 21, 2014 #3
    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 lets you pick an existing file to save the data to OR if a file is already open it saves it there.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Saving string to text file in Java IO
  1. String in java (Replies: 7)

  2. Multithreaded File IO (Replies: 7)

Loading...