天天看点

java怎么修改文件名称_Java修改文件名称

import java.io.File;

import java.io.IOException;

public class Rename {

public static void main(String[] args) throws IOException  {   File oldFile = new File("d:/1.Out");  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 + "1.Out.back");  System.out.println("修改后:"+newFile.getName());  if (oldFile.renameTo(newFile))   {   System.out.println("修改成功!!!");  }   else   {   System.out.println("修改失败!!!");  } }}