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


    一、代码如下

    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 + "
    ";
    					//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();}
    			}
    			
    		}
    	}
    
    }
    
  • 相关阅读:
    微信小程序里使用 Redux 状态管理
    ES6基础
    微信小程序入门
    Redis 安装
    ServiceStack.Redis 使用
    改善C#程序,提高程序运行效率的50种方法
    Jquery Ajax调用aspx页面方法
    WebAPI创建
    Find the Difference -- LeetCode
    Encode and Decode Strings -- LeetCode
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7633140.html
Copyright © 2020-2023  润新知