java:往文件中写数据,新写入的数据总是覆盖原有数据,怎么能实现追加功能呢?
3个回答
展开全部
File file=new File("f:/a.txt");
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(file,true));
bw.write("efg");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里关键代码bw=new BufferedWriter(new FileWriter(file,true));
后面参数的true,就代表即使a.txt里面有内容,也不会替换。
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(file,true));
bw.write("efg");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里关键代码bw=new BufferedWriter(new FileWriter(file,true));
后面参数的true,就代表即使a.txt里面有内容,也不会替换。
展开全部
/**
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;
try{
bufferedReader = new BufferedReader(new FileReader("text1.txt"));
printWriter = new PrintWriter(new FileWriter("text.txt", true));
String l;
while ((l = bufferedReader.readLine()) != null) {
printWriter.print(l);
}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
}
}
希望对你有所帮助
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;
try{
bufferedReader = new BufferedReader(new FileReader("text1.txt"));
printWriter = new PrintWriter(new FileWriter("text.txt", true));
String l;
while ((l = bufferedReader.readLine()) != null) {
printWriter.print(l);
}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
}
}
希望对你有所帮助
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
Creates a file output stream to write to the file represented by the specified File object.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询