问一个java文件读写问题

miaohw2002 2004-09-17 03:32:15
一个txt文件,我把内容读了出来更改后怎么写回去替换原来内容

给点代码提示谢谢
...全文
130 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
andunkafei 2004-09-17
  • 打赏
  • 举报
回复
读出原文件内容并修改完后将原来的文件删除,然后将新文件保存为原来的文件名即可。
registered 2004-09-17
  • 打赏
  • 举报
回复
FileOutputStream
public FileOutputStream(File file,
boolean append)
throws FileNotFoundExceptionCreates a file output stream to write to the file represented by the specified File object. 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 the path represented by the file argument 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:
file - the file to be opened for writing.
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:
1.4
See Also:
File.getPath(), SecurityException, SecurityManager.checkWrite(java.lang.String)


使用这个构造函数创建对象
用 append = false;
miaohw2002 2004-09-17
  • 打赏
  • 举报
回复
BufferedWrite我写的怎么是追加呢?
miaohw2002 2004-09-17
  • 打赏
  • 举报
回复
2楼的你的l是什么l.add(str);
smallMage 2004-09-17
  • 打赏
  • 举报
回复
用BufferedReader读文件,用BufferedWrite写到文件里就可以呢
redlaputa 2004-09-17
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;

class p2
{
public static void main(String[] args)
{
try
{


BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("p1.java")));
DataOutputStream dos=new DataOutputStream(new FileOutputStream("pp.java"));
String str;Object o;
//while(br.ready())
while((str=br.readLine())!=null)
{

l.add(str);
//System.out.println(str);
}
//ListIterator it=l.listIterator();
int i=1;
while ((o=l.removeLast())!=null)
{
System.out.println((String)o);
dos.writeBytes(i+" "+(String)o+"\n");
i++;
}



while(it.hasNext())
{
System.out.println((String)it.next());
}
while(it.hasPrevious())
{
System.out.println((String)it.previous());
}
*/
//System.out.println("Hello World!");
}
catch (Exception e)
{
}

}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧