import java.io.File;
public class exercise7 {
public static void main(String[] args) {
File file=new File("C:\\Users\\24133\\Desktop\\1.png"); //指定文件名及路径
String name="123";
String filepath=file.getAbsolutePath();
System.out.println(filepath);
file.renameTo(new File(name+".jpg")); //改名
}
}
C:\Users\24133\Desktop\1.png
找到了对应的文件类对象但文件名并未改变。
您好,题主。renameTo()函数中传进去的File对象路径需要指定,您这样只给了一个新名字的,默认会将图片输出到代码所在工程目录下,比如:
String sourceFolderPath = "C:\\Users\\24133\\Desktop\\1.png";
String targetFolderPath = "C:\\Users\\24133\\Desktop\\123.png";
boolean result = new File(sourceFolderPath).renameTo(new File(targetFolderPath));
System.out.println("重命名的结果:" + result);
希望可以解决你的问题。