Java_打开/保存文件

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

         int result;
        JFileChooser fileChooser = new JFileChooser(); // 文件选择框
        File file = null; // 接收文件
        result = fileChooser.showSaveDialog(jButton2); // 显示保存框
        if (result == JFileChooser.APPROVE_OPTION) { // 选择的是确定按钮
            file = fileChooser.getSelectedFile(); // 得到选择的文件
            JOptionPane.showMessageDialog(null, "选择的存储文件名称为:" + file.getName(), "保存", JOptionPane.ERROR_MESSAGE);

        } else if (result == JFileChooser.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "没有选择任何文件", "保存", JOptionPane.ERROR_MESSAGE);

        } else {
            JOptionPane.showMessageDialog(null, "操作出现错误", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (file != null) {
            try {
                PrintStream out = new PrintStream(new FileOutputStream(file));
                out.print(jPanel1.getvector());

                out.close();
            } catch (Exception e1) {
            }
        }
                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            JFileChooser fc = new JFileChooser();
            int intRetVal = fc.showOpenDialog(jButton1);
            if (intRetVal == JFileChooser.APPROVE_OPTION) {
                br = new BufferedReader(new FileReader(fc.getSelectedFile().getPath()));
                String temp = br.readLine();
                String s1=new String();
//                jTextArea_Before.setText(null);
                while (temp != null) {
//                    jTextArea_Before.append(temp + " ");
                   s1 += temp;
                    temp = br.readLine();
                }
//                JOptionPane.showMessageDialog(null, s1, "Error", JOptionPane.ERROR_MESSAGE);
                jPanel1.setvector(s1);
                jPanel1.repaint();
               
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "文件打开失败!!!\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

 

 

             

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值