日期:2014-05-20  浏览次数:20679 次

谁能帮忙解释下面的代码(运行异常)

import   java.io.*;
public   class   WriteFile   {

/**
  *   从键盘读取输入行,并将每行返回到一个文件。
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
File   file=new   File(args[0]);
try   {
BufferedReader   in=new   BufferedReader(new   InputStreamReader(System.in));
PrintWriter   out=new   PrintWriter(new   FileWriter(file));
String   s;
System.out.println( "Enter   file   text. ");
System.out.println( "[Type   ctrl-d(or   ctrl-z)   to   stop.] ");
while((s=in.readLine())!=null){
out.println(s);
}
in.close();
out.close();
}   catch   (IOException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}

}

}
//文件的地址?
/*******************************************************************/


import   java.io.*;
public   class   ReadFile   {

/**
  *   读一个文本文件,将每一行返回到标准输出打印该文件。
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
File   file=new   File(args[0]);
try   {
BufferedReader   in   =new   BufferedReader(new   FileReader(file));
String   s;
s=in.readLine();
while(s!=null){
System.out.println( "Read: "+s);
s=in.readLine();
}
in.close();
}   catch   (FileNotFoundException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (IOException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}

}

}

//文件的地址?


------解决方案--------------------
File file=new File(args[0]);

args[0]就是地址,你运行的时候传进去的

javac xxx.java
java xxx xxx.txt
------解决方案--------------------
汗 Eclipse我不会用。。 一直用的jbuilder
------解决方案--------------------
运行配置里有地方写参数的