/*这个程序是从键盘读入,然后再输出到文本文件中,输入exit结束,但是输出的文件中没有东东,而且我想是在输出文本中追加输入的内容,但这个是重写,怎么解决?*/
import java.io.*;
public class Word
{
/**
* @param args
*/
public static void main(String[] args)
{
String str = new String() ;
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out =new PrintWriter(new FileWriter( "c:\\out.txt "));
while ((str=in.readLine())!= "exit ")
{
System.out.println(str);
out.println(str) ;
}
in.close() ;
out.close() ;
}
catch (Exception e)
{
e.printStackTrace() ;
}
}
}
------解决方案--------------------
输入是不是有错啊
------解决方案--------------------
while ((str=in.readLine())!= "exit ")首先这样比较就不正确,你应该用!str.equals( "exit ")
第二:可以用这个文件输出流
FileOutputStream(String fileName,boolean append)
append参数设为true就可以了追加了
------解决方案--------------------
1,PrintWriter out =new PrintWriter(new FileWriter( "c:\\out.txt ")); 改为
PrintWriter out =new PrintWriter(new FileWriter( "c:\\out.txt ",true));
2,while ((str=in.readLine())!= "exit ")改为
while (!(str=in.readLine()).equals( "exit "))
------解决方案--------------------
while ((str=in.readLine())!= "exit "),
改成
while(!((str = in.readLine()).trim().equals( "exit ")))应该就行了