import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
class idlg extends Dialog{                             //输入对话框
     JTextField ar=new JTextField(40);
    Button bt=new Button("确定"),bt1=new Button("导入"),bt2=new Button("保存");
    idlg(){
        super(new Frame(),"输入数组",true);
        setLayout(new FlowLayout(1,20,20));
        add(ar);
        add(bt);
        add(bt1);
        add(bt2);
        setSize(465,200);
        bt.addActionListener(new ActionListener(){                 //输入数组
              public void actionPerformed(ActionEvent e){
String inp = ar.getText();
java.util.StringTokenizer token = new java.util.StringTokenizer(inp, " ,", false);
int[] numbers = new int[token.countTokens()];
for(int i = 0; token.hasMoreTokens(); i++) {
    try {
         numbers[i] = Integer.parseInt(token.nextToken());
    } catch(NumberFormatException nfe) {
         ar.setText("请输入数字,并以逗号或空格分开。");
         return;
    }
 }
                       // inarr=numbers;
                 setVisible(false);
              }
        });
        bt1.addActionListener(new ActionListener(){                 //导入数组
              public void actionPerformed(ActionEvent e){
                String name="";
FileDialog open=new FileDialog(new Frame(),"导入",FileDialog.LOAD);
                if(e.getSource()==bt1){
                      open.show();
                      if(open.getFile()==null){return;}
                      name=open.getDirectory()+File.separator+open.getFile();
                      FileInputStream fis=null;
                      String str=null;
                      try{
                          fis=new FileInputStream(name);
                          int size=fis.available();
                          byte[] bytes=new byte[size];
                          fis.read(bytes);
                          str=new String(bytes);
                      }catch(IOException e1){}
                      finally{
                          try{
                              fis.close();
                          }catch(IOException e2){}
                      }
                      if(str!=null){
                           ar.setText(str);
                      }                                    
                 }                            
              }
        });
        bt2.addActionListener(new ActionListener(){                 //保存数组
              public void actionPerformed(ActionEvent e){
                String name="";
FileOutputStream fos=null;
                String str=ar.getText();
                FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE);
                if(e.getSource()==bt1){
                     save.show();
                     if(save.getFile()==null){return;}
                     name=save.getDirectory()+File.separator+save.getFile();
                     try{
                         fos=new FileOutputStream(name);
                         fos.write(str.getBytes());
                     }catch(IOException e1){}
                     finally{
                         try{
                             fos.close();
                         }catch(IOException e2){}
                     }
                 }
             }
        });
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                dispose();
                System.exit(0);
            }
        });
   }
}
问题是无法保存文件,点击保存、输入文件名保存后,在保存目录下找不到文件。请大虾们帮帮忙!!

解决方案 »

  1.   

    try{ 
      fos=new FileOutputStream(name); 
      fos.write(str.getBytes()); 
    }catch(IOException e1){
      ex.printStackTrace(); // 把你的异常打印出来。呵呵!
      

  2.   

    String str=ar.getText(); 
                    FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE); 
                    if(e.getSource()==bt1){ 
    上面这段是不是应该是像下面:
    String str=ar.getText(); 
                    FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE); 
                    if(e.getSource()==bt2){ 这样应该就ok的拉,别的都没啥问题
      

  3.   

    那个是我后来修改弄错的,本来就是
                   if(e.getSource()==bt2){  
    可以打开“保存”对话框和输入文件名进行保存,但是在保存目录下找不到文件。