Click to See Complete Forum and Search --> : Saving and loading files


The One
02-15-2006, 12:37 PM
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 ?

The One
02-15-2006, 12:44 PM
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 ?

Khalid Ali
02-16-2006, 09:11 PM
Pls refrain from posting same question multiple times.....