基于Java實現(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/測試/b)
- 全中文文件(如:E:/test/a/測試文本.txt)
- 無后綴文件(如:E:/test/a/b/測試文件)
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); } }); } }
四、運行結(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\嗯嗯_20230676190039.jpg ---> E:\test\_20230676190039.jpg, ====== result: true
2024-03-15 19:13:37.989 [main] INFO com.fly.files.FileTest - E:\test\測試_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實現(xiàn)遍歷文件目錄并去除中文文件名的文章就介紹到這了,更多相關(guān)Java遍歷文件目錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java批量下載將多個文件(minio中存儲)壓縮成一個zip包代碼示例
在Java應用程序中有時我們需要從多個URL地址下載文件,并將這些文件打包成一個Zip文件進行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個文件(minio中存儲)壓縮成一個zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11SpringBoot通過token實現(xiàn)用戶互踢功能(具體實現(xiàn))
所謂token,既用戶能夠在一定時間內(nèi)證明自己身份的一長串字符串,這篇文章主要介紹了SpringBoot通過token實現(xiàn)用戶互踢功能,需要的朋友可以參考下2024-04-04一次Spring無法啟動的問題排查實戰(zhàn)之字節(jié)碼篇
最近學習了spring相關(guān)知識,公司項目也用到了spring,下面這篇文章主要給大家介紹了一次Spring無法啟動的問題排查實戰(zhàn)之字節(jié)碼篇的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-045分鐘讓你快速掌握java8 stream常用開發(fā)技巧
這篇文章主要給大家介紹了關(guān)于java8 stream常用開發(fā)技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12