JAVA写TXT文件
时间:2010-09-20 来源:碧海蓝天—Java
import java.io.*;
public class Txt {
public static void main(String[] args){
String s = new String();
String s1 = new String();
StringBuffer sb=new StringBuffer();
try {
File f = new File("C:\\123.txt");
if(f.exists()){
System.out.print("文件存在");
}else{
System.out.print("文件不存在");
f.createNewFile();//不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f));
s="asbddfjslkfjsladfjlska";
sb.append(s);
while((s = input.readLine())!=null){
s1 += s+"\n";
}
System.out.println(s1);
input.close();
s1 += "添加的内容!";
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1+sb.toString());
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}