批量糾本文件名亂碼,檔案名稱亂碼

來源:互聯網
上載者:User

批量糾本文件名亂碼,檔案名稱亂碼

剛剛從部落格: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,他就會把其他的自動命名了
 
教檔案名稱亂碼怎修正

那些名字 應該是下下來之前就已經亂碼了的。。。沒辦法了。。。直接自己改過來吧。。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.