Java sample(File IO)


run file open dialog file save dialog

---(Source List)---

//Example04
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class Example04 extends Frame implements ActionListener {
  private static final String window_title = "File IO";
  private static final int window_width = 640;
  private static final int window_height = 400;
  private static final String btnOpen_title = "Open";
  private static final String btnSave_title = "Save";
  private static final int tf01_size = 80;
  private static final int tf02_size = 20;
  private static final String OpenDialog_title = "File Select";
  private static final String SaveDialog_title = "File Save";
  private static final long serialVersionUID = 1L;
  TextField tf01 = new TextField("", Example04.tf01_size);
  TextField tf02 = new TextField("", Example04.tf02_size);
  // Main (show window)
  public static void main(final String ar[]) {
    final Frame f = new Example04();
    f.setTitle(Example04.window_title);
    f.setSize(Example04.window_width, Example04.window_height);
    f.setVisible(true);
  }
  // Init (set parts)
  Example04() {
    this.setLayout(new FlowLayout());
    final Button btnOpen = new Button(Example04.btnOpen_title);
    final Button btnSave = new Button(Example04.btnSave_title);
    btnOpen.addActionListener(this);
    btnSave.addActionListener(this);
    this.add(btnOpen);
    this.add(btnSave);
    this.add(this.tf01);
    this.add(this.tf02);
    this.addWindowListener(new WinAdapter());
  }
  // close window
  class WinAdapter extends WindowAdapter {
    @Override
    public void windowClosing(final WindowEvent we) {
      System.exit(0);
    }
  }
  public void actionPerformed(final ActionEvent ae) {
    if (ae.getActionCommand() == Example04.btnOpen_title) {
      this.tf01.setText("");
      this.tf02.setText("");
      try {
        final FileInputStream fi = new FileInputStream(this.openfile());
        final BufferedReader br = new BufferedReader(
            new InputStreamReader(fi));
        final String s01 = br.readLine();
        if (s01 != null) {
          this.tf01.setText(s01);
        }
        final String s02 = br.readLine();
        if (s02 != null) {
          this.tf02.setText(s02);
        }
        br.close();
        fi.close();
      } catch (final Exception e) {
      }
    }
    if (ae.getActionCommand() == Example04.btnSave_title) {
      try {
        final FileOutputStream fo = new FileOutputStream(this
            .writefile());
        final PrintStream ps = new PrintStream(fo);
        ps.println(this.tf01.getText());
        ps.println(this.tf02.getText());
        ps.close();
        fo.close();
      } catch (final Exception e) {
      }
    }
  }
  String openfile() {
    final FileDialog fd = new FileDialog(new Frame(),
        Example04.OpenDialog_title);
    fd.setVisible(true);
    final String fullpath = fd.getDirectory() + fd.getFile();
    fd.dispose();
    return fullpath;
  }
  String writefile() {
    final FileDialog fd = new FileDialog(new Frame(),
        Example04.SaveDialog_title, FileDialog.SAVE);
    fd.setVisible(true);
    final String fullpath = fd.getDirectory() + fd.getFile();
    fd.dispose();
    return fullpath;
  }
}