码迷,mamicode.com
首页 > 其他好文 > 详细

IO编程__缓冲字符流__应用__记事本的打开、保存功能

时间:2017-10-06 23:10:46      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:gen   get   ttext   logs   exce   size   out   rri   generated   

 

 

一、代码如下

 

package www.tainiu.wenjian;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class ag__FIleYingYong__V1 extends JFrame implements ActionListener {
	JTextArea jta= null;
	
	JMenuBar jmb= null;
	JMenu jm= null;
	JMenuItem jmi_1= null;
	JMenuItem jmi_2= null;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ag__FIleYingYong__V1 fy= new ag__FIleYingYong__V1();
	}
	
	public ag__FIleYingYong__V1() {
		// TODO Auto-generated constructor stub
		jta= new JTextArea();
		jmb= new JMenuBar();
		jm= new JMenu("文件");
		jm.setMnemonic(‘F‘);
		jmi_1= new JMenuItem("打开");
		jmi_2= new JMenuItem("保存");
	
		this.setJMenuBar(jmb);
		jmb.add(jm);
		jm.add(jmi_1);
		jm.add(jmi_2);
		
		this.add(jta);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 300);
		this.setVisible(true);
		
		//设置监听操作
		jmi_1.addActionListener(this);
		jmi_2.addActionListener(this);
		jmi_1.setActionCommand("打开");
		jmi_2.setActionCommand("保存");
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//System.out.println(e.getActionCommand());
		if(e.getActionCommand() == "打开") {
			JFileChooser jfc_V1= new JFileChooser();
			jfc_V1.setDialogTitle("请选择文件。。。。");
			jfc_V1.showOpenDialog(null);
			jfc_V1.setVisible(true);
			
			//导入文件
			String path_V1= jfc_V1.getSelectedFile().getPath();
			//System.out.println(path_V1);
			BufferedReader br= null;
			try {
				br= new BufferedReader(new FileReader(path_V1));
				String s= "";
				String result= "";
				int n= 0;
				while((s=br.readLine()) != null) {
					result += s + "\r\n";
					//System.out.println(result);
				}
				
				jta.setText(result);
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally {
				try {br.close();} catch (IOException e1) {e1.printStackTrace();}
			}
			
		} else if(e.getActionCommand() == "保存") {
			JFileChooser jfc_V2= new JFileChooser();
			jfc_V2.setDialogTitle("请选择保存路径。。。");
			jfc_V2.showOpenDialog(null);
			jfc_V2.setVisible(true);
			String path_V2= jfc_V2.getSelectedFile().getPath();
			//输出流__写入到硬盘
			BufferedWriter bw= null;
			try {
				bw = new BufferedWriter(new FileWriter(path_V2));
				String result_V2= jta.getText();
				bw.write(result_V2);
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally {
				try {bw.close();} catch (IOException e1) {e1.printStackTrace();}
			}
			
		}
	}

}

 

IO编程__缓冲字符流__应用__记事本的打开、保存功能

标签:gen   get   ttext   logs   exce   size   out   rri   generated   

原文地址:http://www.cnblogs.com/wujianbo123/p/7633140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!