/
修改文件名
@param oldFilePath 原文件路径
@param newFileName 新文件名称
@param overriding 判断标志(如果存在相同名的文件是否覆盖)
@return
/
public static boolean renameFile(String oldFilePath,String newFileName,boolean overriding){
File oldfile = new File(oldFilePath);
if(!oldfileexists()){
return false;
}
String newFilepath = oldfilegetParent()+Fileseparator+newFileName;
File newFile = new File(newFilepath);
if(!newFileexists()){
return oldfilerenameTo(newFile);
}else{
if(overriding){
newFiledelete();
return oldfilerenameTo(newFile);
}else{
return false;
}
}
}
原文链接:网页链接
如有帮助请采纳(不懂请提问),可以看我主页,欢迎来交流学习;
覆盖是override方法重写 重载是overload按照教科书上的定义,重载就是具有相同函数名,返回类型可以不同,参数个数、顺序、类型不同的函数。我的理解是重载是发生在两个或者是更多的函数具有相同的名字的情况下。
重写就是覆盖父类的方法,和父类有相同返回类型,参数,甚至是抛出的异常,重写方法不能为private,运用中最典型的就是对接口方法的覆盖。FileOutputStream 还有一个构造函数:
FileOutputStream (File file, boolean append)
JDK 文档中对此方法的说明:
“如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。”
即可解决你的问题。
改成:FileOutputStream fos = new FileOutputStream(file, true);用这个方法public FileOutputStream(String name,boolean append)throws FileNotFoundException,将逻辑值append设为false时,写入的字节数据覆盖文本原来的内容,为ture是即追加到原来文本的末尾。覆盖原文本内容如:FileOutputStream x=new FileOutputStream("文件全路径",false);throws FileNotFoundException为抛出异常。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)