import java.io.*;
public class ReadWrite { public static void main(String[] args) throws Exception {
String inFileName = ""; // XXX
String outFileName = ""; // XXX
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(outFileName))); String s = null;
while ((s=in.readLine()) != null) out.println(s);
out.close();
in.close();
}}
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
Since:
JDK1.1
See Also:
SecurityManager.checkWrite(java.lang.String)看看上面的你就知道了,只要你构造文件输出流FileOutputStream(String name,
boolean append)
设置好第2个参数就可以了,当append为true时,就是向文件中追加内容,也就是你要实现的功能。
BufferedReader in = new BufferedReader(new FileReader(configpath));
in.readLine();
其中configpath就是你的文件的路径。
你应该明白了,这样你的问题,读一行,和追加不都实现了
FileWriter fw;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
fw = new FileWriter("test.txt");
String str;
System.out.println("Enter Text ('stop' to quit).");
do{
str = br.readLine();
if(str.compareTo("stop") == 0) break;
str = str + "\r\n";
fw.write(str);
} while(str.compareTo("stop" != 0);
fw.close();读出时只要用readLine()方法就可以了。