批量糾本文件名亂碼,檔案名稱亂碼
剛剛從部落格:ym——android源碼大放送(實戰開發必備)中下載了百度網盤中的代碼大全,結果發現檔案名稱字存在嚴重的亂碼。
這一下2G多的資源,扔掉實在太可惜,於是我就決定給他改正過來。
經過測試我發現這些檔案的名字應該是utf-8格式的
於是就行動起來:
import java.io.File;
import java.io.UnsupportedEncodingException;
public class Correct {
/*
* 通過遞迴得到某一路徑下所有的目錄及其檔案
*/
static void correctName(String filePath) throws UnsupportedEncodingException {
File root = new File(filePath);
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
correctName(file.getAbsolutePath());
System.out.println(changName(file.getAbsolutePath())+":"+file.getAbsolutePath());
} else {
System.out.println(changName(file.getAbsolutePath())+":"+file.getAbsolutePath());
}
}
}
/*
* 修改檔案名稱,成功返回true
*/
public static boolean changName(String path) throws UnsupportedEncodingException {
File oldfile = new File(path);
//擷取當前檔案名稱,轉換編碼得到新的檔案名稱
String newName = new String(oldfile.getName().getBytes(), "UTF-8");
File newfile = new File(oldfile.getParent()+File.separator+ newName);
//重新命名
return oldfile.renameTo(newfile);
}
public static void main(String[] args) {
try {
correctName("F://BaiduYunDownload//源碼//源碼");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
總體思想:遞迴得到個個檔案目錄下所有檔案名稱,將檔案名稱重新編碼。
運行後:
很完美了,不過還是有部分亂碼,可能是由於壓縮檔造成的。
怎批量改本文件名亂碼
改亂碼檔案名稱好改,只要你的檔案沒能損壞,你可以把所有的檔案都先上,然後在其中一個檔案上點右鍵然後選重新命名,比如改成1,他就會把其他的自動命名了
教檔案名稱亂碼怎修正
那些名字 應該是下下來之前就已經亂碼了的。。。沒辦法了。。。直接自己改過來吧。。。