Java基础学习之io流的练习(NotePad的打开文件和保存文件)

1、需要一个窗体,如图


2、打开文件:就是弹出打开对话框(使用JFileChooser类),选择文件,将所选文件的内容读取到JTextArea中

3、保存文件:就是弹出保存对话框(使用JFileChooser类),选择保存的路径,创建新的文件,将JTextArea的内容读取到新创建的文件中或者已有的文件中

源代码如下:

NotePadFrame类:

package com.test.notepad;

import javax.swing.JFrame;

/**
 * 窗体类
 * 
 * @author lhz
 * 
 */
public class NotePadFrame extends JFrame {
	public MenuPanel mp;
	public WritePanel wp;

	public void initFrame() {
		this.setSize(800, 800);
		this.setTitle("NotePad");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);

		mp = new MenuPanel();
		this.setJMenuBar(mp);

		wp = new WritePanel();
		this.add(wp);

		this.setVisible(true);
	}
}
MenuPanel类:

package com.test.notepad;

import java.awt.Font;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.BevelBorder;

public class MenuPanel extends JMenuBar {
	public JMenu menu1, menu2, menu3;
	public JMenuItem item1, item2, item3;
	public MenuListener listener;

	public MenuPanel() {
		this.setBorder(new BevelBorder(0));
		this.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		// this.setBackground(bg)
		this.initMenu();
	}

	public void initMenu() {
		this.addM();
	}

	public void addM() {
		menu1 = new JMenu("文件");
		menu2 = new JMenu("编辑");
		menu3 = new JMenu("帮助");
		menu1.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		menu2.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		menu3.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		this.add(menu1);
		this.add(menu2);
		this.add(menu3);

		item1 = new JMenuItem("新建");
		item2 = new JMenuItem("打开");
		item3 = new JMenuItem("保存");
		// item4 = new JMenuItem("剪切");
		// item5 = new JMenuItem("复制");
		// item6 = new JMenuItem("粘贴");
		item1.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		item2.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		item3.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		// item4.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		// item5.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		// item6.setFont(new Font("TimesRoman", Font.PLAIN, 13));
		listener = new MenuListener();
		item1.addActionListener(listener);
		item2.addActionListener(listener);
		item3.addActionListener(listener);
		// item4.addActionListener(listener);
		// item5.addActionListener(listener);
		// item6.addActionListener(listener);
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		// menu2.add(item4);
		// menu2.add(item5);
		// menu2.add(item6);
	}
}
WritePanel类:

package com.test.notepad;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class WritePanel extends JPanel {
	public static JTextArea area;
	public JScrollPane jsp;
	public Dimension dimension = new Dimension(783, 784);

	public WritePanel() {
		this.setPreferredSize(dimension);
		this.setBackground(Color.white);
		this.initWrite();
	}

	public void initWrite() {
		area = new JTextArea();
		jsp = new JScrollPane(area);
		jsp.setPreferredSize(dimension);
		this.add(jsp);
	}
}
MenuListener类:

package com.test.notepad;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class MenuListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if ("新建".equals(command)) {
			WritePanel.area.setText(null);
		} else if ("打开".equals(command)) {
			WritePanel.area.setText(null);
			JFileChooser chooser = new JFileChooser();
			chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);// 只打开文件
			chooser.showOpenDialog(null);// 打开文件选择器
			File file = chooser.getSelectedFile();// 获取文件对象
			try {
				FileReader reader = new FileReader(file);// 字符输入流
				char[] chars = new char[128];
				int count = reader.read(chars);// 获取真正读取字符数
				while (count != -1) {
					String str = new String(chars, 0, count);
					WritePanel.area.append(str);
					count = reader.read(chars);// 获取真正读取字符数
				}
				reader.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else if ("保存".equals(command)) {
			JFileChooser chooser = new JFileChooser();// 文件选择器
			String fileName = null;// 文件名为null
			String path = null;// 文件路径

			File file = new File("D://新建文本文档.txt");// 定义File对象,默认文件
			chooser.setSelectedFile(file);// 设置默认文件名

			int select = chooser.showSaveDialog(null);// 得到int!!!!!!!!!!!!!!!!!必须在设置默认文件名之后不然不起作用

			if (select != JFileChooser.APPROVE_OPTION) {// 如果点击取消或者关闭就退出方法
				return;
			}

			file = chooser.getSelectedFile();// 获取选定文件(鼠标点击选定)
			fileName = chooser.getName(file);// 如果没有选取中任何的文件,将会返回手输入的文件名

			if (file.isFile()) {// 如果选的是文件
				fileName = file.getName();// 获取该文件名
			} else {// 否则是个文件夹
				file = chooser.getCurrentDirectory();// 获得当前目录
				path = file.getPath() + "//" + fileName;// 将获取的目录转为字符串,为获取这个文件夹里的文件名
				file = new File(path);// 以这个路径构造File对象
			}
			if (file.exists()) { // 若选择已存在的文件询问是否要覆盖
				int i = JOptionPane.showConfirmDialog(null, "该文件已经存在,确定要覆盖吗?");
				if (i == JOptionPane.YES_OPTION)
					;// 执行下去
				else
					return;// 退出方法
			}

			try {
				FileWriter writer = new FileWriter(file);// 字符输出流
				String string = WritePanel.area.getText();// 获取文本区的字符串
				char[] chars = string.toCharArray();// 得到字符数组
				writer.write(chars);
				writer.flush();
				writer.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}
FrameTest类:

package com.test.notepad;

public class FrameTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new NotePadFrame().initFrame();
	}

}
效果图:







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值