www.webdeveloper.com
Results 1 to 3 of 3

Thread: Saving and loading files

  1. #1
    Join Date
    Feb 2006
    Posts
    98

    Question Saving and loading files

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    public class load {
    public static void main (String [] args) {

    int Button_width = 65;
    int Button_height = 25;

    JLabel NameLabel = new JLabel ("Name:");
    final JTextField Nametextfield = new JTextField(15);

    JButton Savebutton = new JButton("Save");
    Savebutton.setPreferredSize (new Dimension (Button_width, Button_height));
    JButton Openbutton = new JButton("Open");
    Openbutton.setPreferredSize (new Dimension (Button_width, Button_height));


    class SaveButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
    try {
    FileWriter writer = new FileWriter ("data.txt");
    PrintWriter out = new PrintWriter (writer);

    String AddName = Nametextfield.getText();
    out.println (AddName);
    out.close();
    }
    catch (IOException exception) {
    System.out.println ("can't write to file");
    }

    }
    };
    ActionListener listener = new SaveButtonListener ();
    Savebutton.addActionListener (listener);
    JFrame frame = new JFrame ("Title");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (3,2));
    frame.add (NameLabel);
    frame.add (Nametextfield);
    frame.add (Savebutton);
    frame.pack ();
    frame.show ();

    }
    }
    ********************************************************

    I NEED HELP?
    1) on the above code i can save wat i type in to file that was made (data.txt as seen above). How do i make it so that i can select where i want to save. How can i choose the name and location wen i click on save???

    2) Another question is now wen i save, how do i now load the data???

    Help ?

  2. #2
    Join Date
    Feb 2006
    Posts
    98

    Question Saving and loading files

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    public class load {
    public static void main (String [] args) {

    int Button_width = 65;
    int Button_height = 25;

    JLabel NameLabel = new JLabel ("Name:");
    final JTextField Nametextfield = new JTextField(15);

    JButton Savebutton = new JButton("Save");
    Savebutton.setPreferredSize (new Dimension (Button_width, Button_height));
    JButton Openbutton = new JButton("Open");
    Openbutton.setPreferredSize (new Dimension (Button_width, Button_height));


    class SaveButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
    try {
    FileWriter writer = new FileWriter ("data.txt");
    PrintWriter out = new PrintWriter (writer);

    String AddName = Nametextfield.getText();
    out.println (AddName);
    out.close();
    }
    catch (IOException exception) {
    System.out.println ("can't write to file");
    }

    }
    };
    ActionListener listener = new SaveButtonListener ();
    Savebutton.addActionListener (listener);
    JFrame frame = new JFrame ("Title");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (3,2));
    frame.add (NameLabel);
    frame.add (Nametextfield);
    frame.add (Savebutton);
    frame.pack ();
    frame.show ();

    }
    }
    ********************************************************

    I NEED HELP?
    1) on the above code i can save wat i type in to file that was made (data.txt as seen above). How do i make it so that i can select where i want to save. How can i choose the name and location wen i click on save???

    2) Another question is now wen i save, how do i now load the data???

    Help ?

  3. #3
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Pls refrain from posting same question multiple times.....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles