Notepad练习2

/**
 * Notepad练习
 */

package day150201;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
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.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Notepad {
	private Frame f = null;
	private TextArea ta = null;
	private MenuBar mb = null;
	private Menu fileMenu = null;
	private Menu _1Menu = null;

	private MenuItem _2MenuItem = null;
	private MenuItem openItem = null;
	private MenuItem saveItem = null;
	private MenuItem closeItem = null;

	private static FileDialog fd = null;
	private static File file = null;

	public Notepad() {
		init();
	}

	public void init() {
		f = new Frame("Notepad-yg");
		f.setBounds(200, 100, 600, 400);
		f.setLayout(new BorderLayout());// 可以不设置,frame默认即是

		mb = new MenuBar();
		fileMenu = new Menu("File");
		_1Menu = new Menu("_1Menu");
		_2MenuItem = new MenuItem("_2MenuItem");
		openItem = new MenuItem("Open");
		saveItem = new MenuItem("Save");
		closeItem = new MenuItem("Close");

		_1Menu.add(_2MenuItem);
		fileMenu.add(_1Menu);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(closeItem);
		mb.add(fileMenu);

		ta = new TextArea();

		f.setMenuBar(mb);
		f.add(ta);

		addListener();

		f.setVisible(true);
	}

	private void addListener() {
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		openItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				fd = new FileDialog(f, "Open", FileDialog.LOAD);
				fd.setVisible(true);

				String dir = fd.getDirectory();
				String fileName = fd.getFile();

				if (dir == null || fileName == null)
					return;
				ta.setText("");
				file = new File(dir, fileName);
				BufferedReader bufr = null;
				try {

					bufr = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = bufr.readLine()) != null) {
						ta.append(line + "\r\n");
					}

				} catch (IOException el) {
					throw new RuntimeException("打开文件失败");
				} finally {
					try {
						if (bufr != null)
							bufr.close();
					} catch (IOException el) {
						throw new RuntimeException("读取流关闭失败");
					}
				}
			}
		});

		saveItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (file == null) {
					fd = new FileDialog(f, "Open", FileDialog.SAVE);
					fd.setVisible(true);

					String dir = fd.getDirectory();
					String fileName = fd.getFile();
					if (dir == null || fileName == null)
						return;
					file = new File(dir, fileName);
				}

				BufferedWriter bufw = null;
				try {
					bufw = new BufferedWriter(new FileWriter(file));
					bufw.write(ta.getText());
					bufw.flush();
				} catch (IOException el) {
					throw new RuntimeException("保存文件失败");
				} finally {
					try {
						if (bufw != null)
							bufw.close();
					} catch (IOException el) {
						throw new RuntimeException("写入流关闭失败");
					}
				}
			}
		});

		closeItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});
	}

	public static void main(String[] args) {
		new Notepad();
	}
}


你可能感兴趣的:(文件读写,Notepad练习)