import java.io.File
/*
* This is class used for rename the whole file under file folder name*/
public class RenameFile {
public static void main(String args[]) {
/*
* you should change the path E://文件夹 to what you have on your own computer!*/
File fl = new File("E://文件夹") //这里写上发替换的文件夹路径,注意使用双斜杠
String[] files = fl.list()
File f = null
String filename = ""
for(String file:files){
f = new File(fl,file)//注意,这里一定要写成File(fl,file)如果写成File(file)是行不通的,一定要全路径
filename = f.getName()
// System.out.println(filename)
/*the string 要替换掉的内容 is the content in your own file string with the name 替换成的内容,
* here you should change the string into what you have.*/
f.renameTo(new File(fl.getAbsolutePath() + "//" + filename.replace("要替换掉的内容", "替换成的内容")))//这里可以反复使用replace替换,当然也可以使用正则表达式来替换了
}
}
}
package scriptimport java.io.File
import java.io.IOException
public class Realname {
public static void main(String[] args) throws IOException
{
File oldFile = new File("d:/PMS")
if(!oldFile.exists())
{
oldFile.createNewFile()
}
System.out.println("修改前文件名称是:"+oldFile.getName())
String rootPath = oldFile.getParent()
System.out.println("根路径是:"+rootPath)
File newFile = new File(rootPath + File.separator + "PMSTmp")
System.out.println("修改后文件名称是:"+newFile.getName())
if (oldFile.renameTo(newFile))
{
System.out.println("修改成功!")
}
else
{
System.out.println("修改失败")
}
}
}
原来写的例子~~~希望能采纳。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)