提示:提问和回答别人的问题均可赢得积分
此问题已经被浏览72次,收藏:0次. 所属类别:[
Java]
问题:谁能帮忙解释下面的代码(运行异常)?
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();
}
}
}
//文件的地址?
提问时间:
2007-5-29 14:21:51 回答次数:
(4) 提问人:
软界网友 我来回答
回答(1)
File file=new File(args[0]);
args[0]就是地址,你运行的时候传进去的
javac xxx.java
java xxx xxx.txt
回答(2)
现在Eclipse中运行,怎么传?(args[0]就是地址,你运行的时候传进去的)
回答(3)
汗 Eclipse我不会用。。 一直用的jbuilder
关键字:
TODOAuto,
newFile,
文件,
println,
generatedcatchblock,
printStackTrace,
System,
readLine,
close,
publicstaticvoidmain,
generatedmethodstub,
BufferedReaderin,
newBufferedReader,
IOExceptione,
publicclass,
importjava