Loading

【Java】java输入输出流

 

 

  1 import java.awt.event.ActionEvent;
  2 import java.awt.event.ActionListener;
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.awt.event.*;
  6 import java.io.*;
  7 
  8 class Ex_9_2 extends JFrame implements ActionListener {
  9     JTextArea tArea;
 10     JButton btSave ;
 11     JButton btOpen ;
 12     JScrollPane scrollPaneWithTextArea ;
 13     JFileChooser fileChooser ;
 14     FileFilter fileFilter; //文件匹配
 15     JDialog dialog;
 16     File file;
 17     //FileOutputStream fos;
 18     //FileInputStream fis;
 19     FileReader fis;
 20     FileWriter fos;
 21     BufferedReader bfr;
 22     
 23     static int frameW = 500;
 24     static int frameH = 500;
 25     
 26     Ex_9_2(){
 27         setSize(frameW,frameH);
 28         setTitle("文件保存Test");
 29         tArea = new JTextArea();
 30         btSave = new JButton("保存");
 31         btOpen = new JButton("打开");
 32         scrollPaneWithTextArea = new JScrollPane(tArea) ; //文本区加入滚动条面板
 33         fileChooser = new JFileChooser("D:\\Workspaces\\java\\TestMySelf"); //设定默认文件选择路径
 34         setLayout(null);
 35         scrollPaneWithTextArea.setBounds(0,0,485,300);
 36         this.add(scrollPaneWithTextArea);
 37         btOpen.setBounds(50,330,60,30);
 38         btSave.setBounds(350,330,60,30);
 39         this.add(btSave); this.add(btOpen);
 40         btSave.addActionListener(this);
 41         btOpen.addActionListener(this);
 42         
 43         addWindowListener(new WindowAdapter(){
 44                 public void windowClosing(WindowEvent e){
 45                     System.exit(0);
 46                 }
 47         });
 48     }
 49     @Override
 50     public void actionPerformed(ActionEvent arg0) {
 51         // TODO Auto-generated method stub
 52         int fileChooserReturn;
 53         
 54         //实现打开按钮功能
 55         if (arg0.getSource() == btOpen){
 56             fileChooser.setDialogTitle("打开文件");
 57             fileChooserReturn = fileChooser.showOpenDialog(this); //打开文件选择对话框,并返回点击的按钮值
 58             if (fileChooserReturn == JFileChooser.APPROVE_OPTION){ // 按下了“打开\保存”按钮
 59                 file = fileChooser.getSelectedFile();
 60                 //tArea.setText("选择的文件名是:" + file.getPath());
 61                 try{
 62                     fis = new FileReader(file);
 63                     bfr = new BufferedReader(fis);
 64                 }catch(FileNotFoundException fnfe){
 65                     JOptionPane.showMessageDialog(this, "文件没有找到!");
 66                     return;
 67                 }
 68                 try{
 69                     //读入数据
 70                     String str="";
 71                     while( (str = bfr.readLine()) != null){
 72                         tArea.append(str);
 73                     }
 74                     fis.close();
 75                     JOptionPane.showMessageDialog(this, "打开成功!");
 76                     
 77                 }catch(IOException ioe){
 78                     JOptionPane.showMessageDialog(this, "打开失败!");
 79                     return ;
 80                 }
 81             }
 82         }      
 83         
 84         //实现保存按钮功能
 85         else if (arg0.getSource() == btSave){
 86             fileChooser.setDialogTitle("保存文件");
 87             fileChooserReturn = fileChooser.showSaveDialog(this); //保存文件选择对话框,并返回点击的按钮值
 88             if (fileChooserReturn == JFileChooser.APPROVE_OPTION){ // 按下了“打开\保存”按钮
 89                 file = fileChooser.getSelectedFile();
 90                 //tArea.setText("选择的文件名是:" + file.getPath());
 91                 if (file.exists()){
 92                     int yesOrNo;
 93                     yesOrNo = JOptionPane.showConfirmDialog(this, "文件已经存在!是否保存?");
 94                     if (yesOrNo == JOptionPane.YES_OPTION){
 95                         try{
 96                             fos = new FileWriter(file);
 97                         }catch(IOException fnfe){
 98                             tArea.setText("没有找到文件\n" + fnfe.getMessage());
 99                             return ;
100                         }
101                         try{
102                             //写入文件
103                             fos.write(tArea.getText()); 
104                             fos.close();
105                             JOptionPane.showMessageDialog(this, "保存成功!");
106                         }catch(IOException ioe){
107                             JOptionPane.showMessageDialog(this, "保存失败!");
108                             tArea.setText("文件保存失败\n"+ioe.getMessage());
109                             return ;
110                         }
111                     }else {
112                         JOptionPane.showMessageDialog(this, "已存在的文件,未保存!");
113                     }
114                 }
115                 else{
116                     try{
117                         fos = new FileWriter(file);
118                     }catch(IOException fnfe){
119                         tArea.setText("没有找到文件\n" + fnfe.getMessage());
120                         return ;
121                     }
122                     try{
123                         //写入文件
124                         fos.write(tArea.getText()); 
125                         fos.close();
126                         JOptionPane.showMessageDialog(this, "保存成功!");
127                     }catch(IOException ioe){
128                         JOptionPane.showMessageDialog(this, "保存失败!");
129                         tArea.setText("文件保存失败\n"+ioe.getMessage());
130                         return ;
131                     }
132                 }
133             }
134         }
135     }
136 
137 }

 

posted @ 2016-03-17 16:51  uzipi  阅读(180)  评论(0)    收藏  举报