热门

最新

红包

立Flag

投票

同城

我的

发布
2301_80538928
ZZM的爹
2 年前
true2301_80538928

你可以使用 Java 的字符串处理方法来提取文件名和更改扩展名。下面是一个示例代码:

```java
public class FileManipulation {
public static void main(String[] args) {
String filePath = "e:\\myfile\\txt\\result.txt";

// 提取文件名
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);

// 更改扩展名
String newFileName = changeExtension(fileName, "java");

// 打印原文件名和更改后的文件名
System.out.println("原文件名: " + fileName);
System.out.println("更改后的文件名: " + newFileName);
}

// 更改文件扩展名的辅助方法
private static String changeExtension(String fileName, String newExtension) {
int dotIndex = fileName.lastIndexOf(".");
if (dotIndex != -1) {
return fileName.substring(0, dotIndex) + "." + newExtension;
}
return fileName + "." + newExtension;
}
}
```

这段代码会提取出路径中的文件名,并更改扩展名为 "java"。你可以根据实际需要将其集成到你的项目中。

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
下班下班
立即登录