public
FileOutputStream(String
name,
boolean
append)
throws
FileNotFoundException
只需要使用该构造方法在构造FileOutputStream对象时,将第二个参数append的值设置为true即可。
try
{BufferedWriter
out
=
new
BufferedWriter(new
FileWriter(outfilename))
out.write(aString)
out.close()
}
catch
(IOException
e)
{}这种情况就是覆盖的try
{BufferedWriter
out
=
new
BufferedWriter(new
FileWriter(filename,
true))
out.write(aString)
}
catch
(IOException
e)
{}这种情况是添加
解决方案
你的意思就是不覆盖之前的内容,追加写到文件里面 那就FileWriter a=new FileWriter("d:/as.txt")改成 FileWriter a=new FileWriter("d:/as.txt",true)
补充:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
这样每次都输入两个文件的路径不会太麻烦了吗?两个文件名不同的时候也必须要覆盖吗?如果是你自己要用得程序可以说一下你想要用这个程序干什么,多半有更好的方法
//以下程序删除“被覆盖”文件,把另一个文件移动到“被覆盖”文件的目录下
import java.util.*
import java.io.File
import java.sql.Timestamp
public class InputTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in)
System.out.println("输入一号文件的路径")
String path1 = in.nextLine()
System.out.println("输入二号文件的路径")
String path2 = in.nextLine()
File f1 = new File(path1)
File f2 = new File(path2)
long size1 = f1.length()// 大小 bytes
long size2 = f2.length()// 大小
long t1 = f1.lastModified()// 修改时间
long t2 = f2.lastModified()
System.out.println("一号文件的大小是" + size1 + "最后修改时间是"
+ new Timestamp(t1).toString())
System.out.println("二号文件的大小是" + size2 + "最后修改时间是"
+ new Timestamp(t2).toString())
System.out.println("你要覆盖哪个文件?输入的文件号将是被覆盖的文件号(1+回车,或者 2+回车)")
int p1 = in.nextInt()
try {
if (p1 == 1) {
f1.delete()
f2.renameTo(new File(f1.getParentFile(), f2.getName()))
} else {
f2.delete()
f1.renameTo(new File(f2.getParentFile(), f1.getName()))
}
} catch (Exception e) {
System.out.println("IO出错")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)