swing 插件实现简单的文件读取与保存

图像化界面实现对文件的操作,编程实现GUI菜单栏快速访问

package notepade;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
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.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Mynotepade extends JFrame{
    JMenuBar bar= new JMenuBar();//定义菜单栏
    JMenuItem item_open=new JMenuItem("open(O)");
    JMenuItem item_save_as=new JMenuItem("save as(S)");
    JMenu menu_file=new JMenu("file(F)");
    JTextArea j=new JTextArea();
    JScrollPane js=new JScrollPane(j);
    public Mynotepade() {
        this.setSize(500,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        init();
        this.setJMenuBar(bar);
        this.getContentPane().add(js);
    }
    private void init(){
        item_open.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showOpenDialog(Mynotepade.this)==JFileChooser.APPROVE_OPTION) {//
                    File file = chooser.getSelectedFile();
                    j.setText(file.getName()+":"+file.getPath()+"\n"+file.length());
                    readFile(file);
                };
            }
        });
        item_save_as.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showSaveDialog(Mynotepade.this)==JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    writeFile(file.getPath());
                }

            }
        });
        menu_file.setMnemonic(KeyEvent.VK_F);//Alt+F键盘快速访问
        item_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_MASK));//Ctrl+O快速访问
        item_save_as.setMnemonic(KeyEvent.VK_S);//英文状态下输入s快速访问
        menu_file.add(item_open);
        menu_file.add(item_save_as);
        bar.add(menu_file);
    }
    public void readFile(File file){//读文件
        BufferedReader bReader;
        try {
            bReader=new BufferedReader(new FileReader(file));
            StringBuffer sBuffer=new StringBuffer();
            String str;
            while((str=bReader.readLine())!=null){
                sBuffer.append(str+'\n');
                System.out.println(str);
            }
            j.setText(sBuffer.toString());
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public void writeFile(String savepath){//写文件
        FileOutputStream fos= null;
        try {
            fos=new FileOutputStream(savepath);
            fos.write(j.getText().getBytes());
            fos.close();
            System.out.println("已保存");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        j.getText();
    }
    public void copyFile(File file){//复制文件
        File to=new File(file.getAbsolutePath()+"_copy");
        if (file.isFile()) {
            byte[] buf = new byte[1024];//字节流
            int length=0;
            try {
                FileInputStream in=new FileInputStream(file);
                FileOutputStream out=new FileOutputStream(to);
                while((length=in.read(buf))>0){
                    out.write(buf,0,length);
                }
                out.flush();
                in.close();
                out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//适应系统的显示风格
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Mynotepade mynotepade= new Mynotepade();
        mynotepade.setVisible(true);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值