修改文件夹下,文件名编码

<?php
//下载的源文件是utf-8编码,
//但是确用gbk浏览,所以乱码
//在这里把文件名修改为gbk编码
header("content-Type: text/html; charset=GBK");
//变量文件夹
$dir = opendir("file");
 
$path="file/";
//列出 images 目录中的文件
while (($old_name = readdir($dir)) !== false)
{
	echo "filename: " . $old_name . "<br />";
	$new_name=mb_convert_encoding($old_name,"GBK", "UTF-8")if (file_exists($path.$old_name)){ 
		if (rename($path.$old_name,$path.$new_name )) //把原文件重新命名
			{echo "修改成功!";} 
	}else{ print $old_name."文件不存在!<br>" ; } 
		
	
}
closedir($dir);
exit;
 
?>
您可以使用以下Java代码获取指定文件夹中所有中文文件的名称: ``` import java.io.File; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; public class GetChineseFileNames { public static void main(String[] args) { String folderPath = "your/folder/path"; List<String> fileNames = getChineseFileNames(folderPath); for (String fileName : fileNames) { System.out.println(fileName); } } public static List<String> getChineseFileNames(String folderPath) { File folder = new File(folderPath); File[] files = folder.listFiles(); List<String> fileNames = new ArrayList<>(); for (File file : files) { if (file.isFile()) { String fileName = file.getName(); if (isChinese(fileName)) { fileNames.add(decodeFileName(fileName)); } } } return fileNames; } public static boolean isChinese(String str) { for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= 0x4e00 && str.charAt(i) <= 0x9fa5) { return true; } } return false; } public static String decodeFileName(String fileName) { try { return new String(fileName.getBytes("ISO-8859-1"), StandardCharsets.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return fileName; } } ``` 在上述代码中,我们定义了一个 `getChineseFileNames()` 方法来获取文件夹中所有中文文件的名称。首先,我们使用 `isChinese()` 方法判断文件名是否包含中文字符。如果是,我们使用 `decodeFileName()` 方法将ISO-8859-1编码文件名转换为UTF-8编码文件名,并添加到 `fileNames` 列表中。最后,我们返回 `fileNames` 列表。您可以根据实际情况修改该代码以适应您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值