不知道哪里出问题了,不能在原有的文本文件  追加新内容;如果追加的话  原来的文本内容  都没有了,,急急急
import java.util.*;
import java.io.*;
public class Wenjian02 {
static PrintWriter outputStream1=null;
static PrintWriter outputStream2=null;
static boolean flag=true;
public static void main(String args[]){
 try{
outputStream1=new PrintWriter(new FileOutputStream("out1.txt"));
}
catch(FileNotFoundException e){
System.out.println("Error opening the file");
System.exit(0);
}
System.out.println("please enter three lines:");
Scanner key=new Scanner(System.in);
for(int i=0;i<3;i++){
String str=key.nextLine();
outputStream1.println(i+"  "+str);
}
outputStream1.close();
System.out.println("there have three lines in this text");
System.out.println("你想追加文件吗");
String an=key.nextLine();
if(an.equalsIgnoreCase("yes")){
try{
outputStream2=new PrintWriter(new FileOutputStream("out1.txt"),flag);
}
catch(FileNotFoundException e){
System.out.println("you have an error bahavior");
System.exit(0);
}
Scanner key1=new Scanner(System.in);
String newText=key1.nextLine();
outputStream2.println(newText);
outputStream2.close();
}
else System.out.println("xx");
}}

解决方案 »

  1.   

    new PrintWriter(new FileWriter("out1.txt", true));
      

  2.   

    改为
    true
    后  还是原来的问题
      

  3.   

    看清楚一点。true是传递到FileWriter/FileOutputStream的构造方法里,不是PrintWriter的构造方法里。
      

  4.   

    看看这个,
    package FILE;import java.io.*;public class FileAdd {
    public static void appendMethodB(String fileName, String content) {
    try {
    // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
    FileWriter writer = new FileWriter("a.txt", true);//a.txt创建到默认的包名下的文件夹下;
    System.out.println();
    writer.write(content);
    writer.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args)
    {
    FileAdd fa = new FileAdd();
    appendMethodB("a.txt","nih");
    System.out.println("添加成功!");
    }
    }
      

  5.   

    new PrintWriter(new FileWriter("out1.txt", true)); 你创建的 输入流 参数不对