I currently use serialization of objects to save data to disk. It is very
simple.
For saving:
File f = chooser.getSelectedFile();
try
{
FileOutputStream fileStream = new FileOutputStream(f);
ObjectOutputStream objectStream = new ObjectOutputStream
(fileStream);
objectStream.writeObject(myObject);
objectStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
For reading:
File f = chooser.getSelectedFile();
try
{
FileInputStream fileStream = new FileInputStream(f);
ObjectInputStream objectStream = new ObjectInputStream
(fileStream);
MyObject anObject = (MyObject)objectStream.readObject();
objectStream.close();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
> I have started something basic.With the code below it compiles and
runs.It
> has a text field, a horizontal and vertical scroll bar and a basic pull
> down menu.On the menu is open, new, save, saveas.. etc...My question is,
> how would I go about writing the code for the save or saveas.. item on
the
> pull down menu , that would actually save my text to a Floppy disk.:)Also
> where would I start to look for this kind of information and what is it
> called?Any recommended books? Thank You
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.event.*;
>
> public class MyMenu3 extends JFrame {
>
> JTextArea textArea;
> JScrollPane scrollPane;
>
> public MyMenu3 () {
>
> JMenuBar menuBar;
> JMenu menu;
> JMenuItem menuItem;
>
> addWindowListener(new WindowAdapter()
> { public void windowClosing(WindowEvent e)
> { System.exit(0);
> }
> } );
>
> //Add Components
>
> Container contentPane = getContentPane();
> JPanel panel = new JPanel();
> textArea = new JTextArea(300, 300);
> scrollPane = new JScrollPane(textArea,
> ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
> ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
> contentPane.add(panel, "East");
> contentPane.add(scrollPane);
>
> //Create Menu Bar
>
> menuBar = new JMenuBar();
> setJMenuBar(menuBar);
>
> //Build Menu
>
> menu = new JMenu("File");
> menuBar.add(menu);
>
> //Make menu Item
>
> menuItem = new JMenuItem("Save");
> menu.add(menuItem);
> }
>
>
> public static void main(String[] args) {
> MyMenu3 window = new MyMenu3 ();
> window.setSize(300, 300);
> window.setVisible(true);
> }
>
> }
>