不知道哪里出问题了,不能在原有的文本文件 追加新内容;如果追加的话 原来的文本内容 都没有了,,急急急
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");
}}
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");
}}
解决方案 »
- 问:Struts2.0 关于重定向的问题
- JAVA
- SWT 中的 shell窗体,屏蔽X键,就是最大化最小化后面的
- JBPM数据库同步
- 调试uddi4j遇到的问题,希望得到解决,已经迷茫好多天了
- tomcat 如何调用应用服务器端的 EJB 组件方法?
- activemq如何实现多个生产者一个消费者?
- 一个有关搜索的比较有难度问题,困扰了很久!大虾们帮一下忙!多谢!
- 工作流:求XPDL解析器的C#版本或Java版本
- springboot项目部署docker出错、sqlSessionFactory的类路径中定义的资源、异常是com.mysql.jdbc4通信异常:通信链路失败
- 菜鸟问题
- 用java 写.mpp文件
true
后 还是原来的问题
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("添加成功!");
}
}