容易文档读取与保存

简单文档读取与保存
运行界面:
容易文档读取与保存

这个项目 主要是练习运用文件的读写操作 理解并运用I/O流

一: 创建主界面 添加JTextArea文本域  和  菜单栏
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileUI extends JFrame {
	JTextArea text;
	public static void main(String[] args) {
		FileUI ui = new FileUI();
		ui.InitUI();
	}

	public void InitUI() {
		this.setTitle("文本读写");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500, 600);
		this.setLocation(400, 150);

		// 设置文件菜单
		JMenuBar mb = new JMenuBar();
		JMenu menu = new JMenu("File");
		JMenuItem open = new JMenuItem("打开文件");
		open.setActionCommand("open");
		JMenuItem save = new JMenuItem("保存文件");
		save.setActionCommand("save");

		// 设置带滚动条的JTextArea
		 text = new JTextArea();
		JScrollPane scr = new JScrollPane(text);
		this.add(scr);

		this.setJMenuBar(mb);
		mb.add(menu);
		menu.add(open);
		menu.add(save);
		this.setVisible(true);
       }
}


二:为菜单项添加匿名监听器
在InitUI() 中创建匿名动作监听器
// 定义匿名内部类动作监听器
ActionListener lis = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str = e.getActionCommand();
				if ("open".equals(str)) {
					JFileChooser chooser = new JFileChooser();
					// 文件和文件夹可以同时选择
				//	chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
					int t = chooser.showOpenDialog(null);
					//当选择确定按钮时
					if (t == JFileChooser.APPROVE_OPTION) {
						String filename = chooser.getSelectedFile().getAbsolutePath();
						try {
							FileInputStream fls=new FileInputStream(filename);
							BufferedInputStream bis=new BufferedInputStream(fls);
						    byte by[]=new byte[bis.available()];
							bis.read(by);
							String s=new  String(by);
							text.setText(s);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					}
				} else if ("save".equals(str)) {
					JFileChooser chooser = new JFileChooser();
					FileNameExtensionFilter filter = new FileNameExtensionFilter(
					        "文本文档", "txt");
					chooser.setFileFilter(filter);
					//String aa=chooser.getFileFilter().getDescription();
					//System.out.println(aa);
					// 文件和文件夹可以同时选择
				    //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
					int t=chooser.showSaveDialog(null);
					if (t == JFileChooser.APPROVE_OPTION) {
						String filename = chooser.getSelectedFile().getAbsolutePath();
						if(!filename.endsWith(".txt"))
							filename=filename+".txt";
						try {
							FileOutputStream fos=new FileOutputStream(filename);
							BufferedOutputStream bos=new BufferedOutputStream(fos);
							String s=text.getText();
							byte[] ch=s.getBytes();
							bos.write(ch);
							bos.flush();
							bos.close();
							
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		};

		// 为菜单项添加动作监听器
		open.addActionListener(lis);
		save.addActionListener(lis);


注:①当点击打开文件时 创建JChooser 并显示系统提供的打开文件对话框showOpenDialog
   ②当点击保存文件时 创建JChooser 并显示系统提供的保存文件对话框showSaveDialog
   ③通过FileInputStream和FileOutputStream对文件进行读取与保存