import java.awt.*;
import java.awt.event.*;
import java.io.*;public class MyNotepad extends WindowAdapter implements ActionListener
{
Frame f;
Button b1;
TextArea ta;
String fileName;

public static void main(String args[])
    {
     new MyNotepad(args[0]);
    }
    public MyNotepad(String fileName)
    {
     this.fileName=fileName;
     f=new Frame(fileName);
     f.addWindowListener(this);
    
     b1=new Button("存储文件");
     b1.addActionListener(this);
    
     ta=new TextArea(10,40);
    
     f.add(ta,BorderLayout.CENTER);
     f.add(b1,BorderLayout.SOUTH);
    
     f.pack();
     f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
     try
     {
     //以写方式打开文件,可以追加
     FileOutputStream fout=new FileOutputStream(fileName,true);
     byte buf[]=ta.getText().getBytes(); //将文本区中输入的字符串转换为字节数组
     fout.write(buf); //一次性重定向写出到文件中去
     fout.close(); //关闭输入流对象,让输入流对象和文件的关联取消
    }
    catch (Exception ex)
    {
     ex.printStackTrace();
    }
    }
    public void windowClosing(WindowEvent e)
    {
     System.exit(0);
    }
}
----------------------------------
小弟刚刚学习JAVA ,这是个输入输出流的例子,我用JCREATOR打开,编译没问题,可是运行的话
系统提示:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
        at MyNotepad.main<Mynotepad.java:14>
请问是怎么回事呢?

解决方案 »

  1.   

    new MyNotepad(args[0]);这一行需要键盘输入一个参数~^_^
      

  2.   

    谢谢~
    能告诉我怎么设置JCreator能在执行程序的弹出main参数的输入窗口吗?
      

  3.   

    public static void main(String args[])
        {
        new MyNotepad(args[0]);
        }//这里的args[]参数读的应该是dos命令下的指令字符,所以不用指令执行而在各种IDE中执行
    //都会报这个错Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    //因为不存在args[0]
      

  4.   

    arg[0]是引用命令行参数,你不妨设定一个串代替一下测试下程序看有否问题不就行了.