基于Java實(shí)現(xiàn)遍歷文件目錄并去除中文文件名
一、原始需求
需要遍歷文件目錄及其子目錄,找出包含中文字符的文件名,將中文字符去除。
二、maven依賴
pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!--Test-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
三、核心代碼
注意處理:
- 中文路徑(如:E:/test/a/測(cè)試/b)
- 全中文文件(如:E:/test/a/測(cè)試文本.txt)
- 無后綴文件(如:E:/test/a/b/測(cè)試文件)
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FileTest
{
/**
* 中文正則
*/
private String REGEX_CHINESE = "[\u4e00-\u9fa5]";
/**
* 移除中文文件名
*/
@Test
public void removeChineseTest()
{
File dir = new File("E:\\test");
List<File> files = FileUtils.listFiles(dir, null, true).stream().filter(f -> f.isFile()).collect(Collectors.toList());
files.stream().forEach(file -> {
String srcName = file.getName();
String destName = RegExUtils.removePattern(srcName, REGEX_CHINESE);
if (!StringUtils.equals(srcName, destName))
{
// 處理全中文和無后綴文件
if (StringUtils.startsWith(destName, ".") || StringUtils.isBlank(destName))
{
destName = DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd") + destName;
}
// 文件前后綴
String prefix = StringUtils.substringBeforeLast(destName, ".");
String suffix = StringUtils.substring(destName, prefix.length());
// 處理重名
int index = 1;
File destFile = new File(file.getParent() + File.separator + destName);
while (destFile.exists())
{
destName = String.format("%s_%s%s", prefix, index++, suffix);
destFile = new File(file.getParent() + File.separator + destName);
}
Boolean result = file.renameTo(destFile);
log.info("{} ---> {}, ====== result: {}", file.getAbsolutePath(), destFile.getAbsolutePath(), result);
}
});
}
}
四、運(yùn)行結(jié)果
2024-03-15 19:13:37.983 [main] INFO com.fly.files.FileTest - E:\test\a\b\_內(nèi)容20230676190031.jpg ---> E:\test\a\b\_20230676190031.jpg, ====== result: true
2024-03-15 19:13:37.986 [main] INFO com.fly.files.FileTest - E:\test\a\文件_20230676190035.jpg ---> E:\test\a\_20230676190035.jpg, ====== result: true
2024-03-15 19:13:37.987 [main] INFO com.fly.files.FileTest - E:\test\中文_20230676154641.jpg ---> E:\test\_20230676154641.jpg, ====== result: true
2024-03-15 19:13:37.988 [main] INFO com.fly.files.FileTest - E:\test\哈哈_20230676154717.png ---> E:\test\_20230676154717.png, ====== result: true
2024-03-15 19:13:37.988 [main] INFO com.fly.files.FileTest - E:\test\嗯嗯_(tái)20230676190039.jpg ---> E:\test\_20230676190039.jpg, ====== result: true
2024-03-15 19:13:37.989 [main] INFO com.fly.files.FileTest - E:\test\測(cè)試_20230676154623.jpg ---> E:\test\_20230676154623.jpg, ====== result: true
2024-03-15 19:13:37.989 [main] INFO com.fly.files.FileTest - E:\test\維修_20230676155003.jpg ---> E:\test\_20230676155003.jpg, ====== result: true
到此這篇關(guān)于基于Java實(shí)現(xiàn)遍歷文件目錄并去除中文文件名的文章就介紹到這了,更多相關(guān)Java遍歷文件目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包代碼示例
在Java應(yīng)用程序中有時(shí)我們需要從多個(gè)URL地址下載文件,并將這些文件打包成一個(gè)Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11
如何將eclipse項(xiàng)目導(dǎo)入到idea的方法步驟(圖文)
這篇文章主要介紹了如何將eclipse項(xiàng)目導(dǎo)入到idea的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
SpringBoot通過token實(shí)現(xiàn)用戶互踢功能(具體實(shí)現(xiàn))
所謂token,既用戶能夠在一定時(shí)間內(nèi)證明自己身份的一長串字符串,這篇文章主要介紹了SpringBoot通過token實(shí)現(xiàn)用戶互踢功能,需要的朋友可以參考下2024-04-04
解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問題
這篇文章主要介紹了解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
一次Spring無法啟動(dòng)的問題排查實(shí)戰(zhàn)之字節(jié)碼篇
最近學(xué)習(xí)了spring相關(guān)知識(shí),公司項(xiàng)目也用到了spring,下面這篇文章主要給大家介紹了一次Spring無法啟動(dòng)的問題排查實(shí)戰(zhàn)之字節(jié)碼篇的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
5分鐘讓你快速掌握java8 stream常用開發(fā)技巧
這篇文章主要給大家介紹了關(guān)于java8 stream常用開發(fā)技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

