IO编程__缓冲字符流__应用__记事本的打开保存功能
Posted 18513757531
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 + "\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编程__缓冲字符流__应用__记事本的打开保存功能的主要内容,如果未能解决你的问题,请参考以下文章