zoukankan      html  css  js  c++  java
  • FileReader和FileWriter

    public class Ftest extends JFrame{
        private static final long sertalVersionUID = 1L;
        private JPanel jContentPane = null;
        private JTextArea jTextArea = null;
        private JPanel controlPane = null;
        private JButton openButton = null;
        private JButton closeButton = null;
        
        private JTextArea geJTextArea(){
            if(jTextArea == null){
                jTextArea = new JTextArea();
            }
            return jTextArea;
        }
        private JPanel getControlPanel(){
            if(controlPane == null){
                controlPane = new JPanel();
                controlPane.setLayout(new FlowLayout());
                controlPane.add(getOpenButton(),null);
                controlPane.add(getCloseButton(),null);
            }
            return controlPane;
        }
        private JButton getOpenButton() {
            if(openButton == null){
                openButton = new JButton();
                openButton.setText("写入文件");
                openButton.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        File file = new File("word.txt");
                        try {
                            FileWriter out = new FileWriter(file);
                            String s = jTextArea.getText();
                            out.write(s);
                            out.close();
                        } catch (Exception e1) {
                            // TODO: handle exception
                            e1.printStackTrace();
                        }
                    }
                });
            }
            return openButton;
        }
        
        private JButton getCloseButton(){
            if(closeButton == null){
                closeButton = new JButton();
                closeButton.setText("读取文件");
                closeButton.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        File file = new File("word.txt");
                        try {
                            FileReader in = new FileReader(file);
                            char byt[] = new char[1024];
                            int len = in.read(byt);
                            jTextArea.setText(new String(byt,0,len));
                            in.close();
                        } catch (Exception e1) {
                            // TODO: handle exception
                            e1.printStackTrace();
                        }
                    }
                });
            }
            return closeButton;
        }
        
        private void initialize(){
            this.setSize(300,200);
            this.setContentPane(getJContentPane());
            this.setTitle("JFrame");
        }
        
        private JPanel getJContentPane(){
            if(jContentPane == null){
                jContentPane = new JPanel();
                jContentPane.setLayout(new BorderLayout());
                jContentPane.add(geJTextArea(),BorderLayout.CENTER);
                jContentPane.add(getControlPanel(), BorderLayout.SOUTH);
            }
            return jContentPane;
        }
        
        public Ftest(){
            super();
            initialize();
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Ftest thisClass = new Ftest();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true);
        }
    
    }
  • 相关阅读:
    VS2010 枚举注释任务
    osg例子中文翻译,半机翻
    怎么愉快地添加目标位置?
    变更路线节点。妈妈,我的强迫症有救啦!
    测试必备工具之抓包神器 Charles 如何抓取 https 数据包?
    测试必备工具之最强抓包神器 Charles,你会了么?
    ‘员工拒绝加班被判赔偿公司 1.8 万元’,作为测试猿你怕了么?
    全网最全测试点总结:N95 口罩应该如何测试?
    测试角度:如何看待三星大量手机系统崩溃并数据丢失事件?
    男生 vs 女生,谁更加适合做软件测试?
  • 原文地址:https://www.cnblogs.com/dulute/p/10599082.html
Copyright © 2011-2022 走看看