@TOC
一、使用File类进行文件重命名
使用File类的renameTo()
方法来重命名文件,接受一个File对象作为参数,指定新的文件名和路径。
import java.io.File;
public class FileRenameExample {
public static void main(String[] args) {
File oldFile = new File("oldfile.txt");
File newFile = new File("newfile.txt");
if (oldFile.renameTo(newFile)) {
System.out.println("File renamed successfully");
} else {
System.out.println("Failed to rename file");
}
}
}
应用场景1:文件上传到服务器后保持原有文件名
文件上传到服务器时,为了避免出现重复文件名,通常会在文件后缀加上日期与时间,但如果我们需要保持原有文件名,可以按以下方式处理。
示例1:如何将newFileNamePath中的文件名称,改成originalFilename
// 新文件路径,此处假设newFileName为带目录的完整新文件名,profile为需要替换的路径部分
String newFileNamePath = newFileName.replace("/profile", profile);
// 从上传的文件对象直接获取原始文件名
String originalFilename = file.getName();
将新的文件路径转换为File对象newFile,然后使用getParent()
方法获取文件所在的目录路径,再将originalFilename添加到该路径中,创建一个新的File对象originalFile。最后,使用renameTo()方法将newFile重命名为originalFile。
//获取新上传的文件对象
File newFile = new File(newFileNamePath);
//获取新文件的父目录并结合原始文件名创建File对象,准备重命名
File originalFile = new File(newFile.getParent(), originalFilename);
// 尝试重命名文件,实现保留原文件名
if (newFile.renameTo(originalFile)) {
System.out.println("文件重命名成功,保留了原始文件名:" + originalFilename);
} else {
System.out.println("文件重命名失败,请检查文件权限或目标文件是否已存在。");
}
二、从字符串中提取文件路径
Java可以使用字符串的substring()方法来提取子字符串,接着用lastIndexOf()
方法找到反斜杠字符的最后一个出现位置,最后用substring()方法从字符串中提取路径。
public class FilePathExample {
public static void main(String[] args) {
String filePath = "C:\\Users\\User\\Documents\\file.txt";
int lastIndexOf = filePath.lastIndexOf("\\");
String path = filePath.substring(0, lastIndexOf);
//Path: C:\Users\User\Documents
System.out.println("Path: " + path);
}
}
应用场景2:只获取不带http前缀的文件路径url
文件上传到服务器时,会返回一个文件路径url,通常是带http前缀的。
示例2:调用不带http前缀的文件url
indexOf()
方法返回子字符串在原字符串中的起始位置,然后substring()
方法从该位置开始提取子字符串,
提取出/profile/upload/2023/08/11/test_20230811111940A001.hex。
String hexdata = "http://localhost:8080/profile/upload/2023/08/11/test_20230811111940A001.hex";
String subString = hexdata.substring(hexdata.indexOf("/profile"));
应用场景3:获取文件路径url下的其他文件
示例3:通常来说文件都存储在同一个路径下,如何获取同路径下的文件?
使用File类的getParent()
方法来获取文件夹路径,然后使用字符串拼接操作来构建新的路径,会变成E:\ftpTest\upload\2023\08\11/hex2bin.exe。
String hexFilePath = "E:/ftpTest/upload/2023/08/11/test_20230811111940A001.hex";
String parentPath = new File(hexFilePath).getParent();
String hex2binPath = parentPath + "/hex2bin.exe";
三、解决反斜杠字符的问题
在Java里,反斜杠字符(\
)是一个转义字符,用于表示特殊字符。如果想表示一个反斜杠字符,需使用双反斜杠\\
进行转义:
public class FilePathExample {
public static void main(String[] args) {
String filePath = "C:\\Users\\User\\Documents\\file.txt";
int lastIndexOf = filePath.lastIndexOf("\\");
//从字符串filePath的开头一直提取到倒数第二个反斜杠(不包括最后一个反斜杠)为止的文本
String path = filePath.substring(0, lastIndexOf);
System.out.println("Directory Path: " + path);
}
}
Directory Path: C:\Users\User\Documents
应用场景4:将Windows下的文件路径转为Java能够识别的路径
示例4:示例3中获取的文件路径不对,如何解决
示例3中得到的路径是E:\ftpTest\upload\2023\08\11/hex2bin.exe不对,需要使用字符串的 replace() 方法来将所有的反斜杠字符替换为斜杠字符 "/",得到E:/ftpTest/upload/2023/08/11/hex2bin.exe"。:
String hexFilePath = "E:/ftpTest/upload/2023/08/11/test_20230811111940A001.hex";
String parentPath = new File(hexFilePath).getParent().replace("\\", "/");
String hex2binPath = parentPath + "/hex2bin.exe";