public class TransferProject{
public static void transferFile(String pathName,intdepth)throwsException{
File dirFile = new File(pathName);
if (!isValidFile(dirFile)) return;
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
int currentDepth = depth + 1;
for (int i = 0; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
String name = file.getName();
//如果是一个目录,搜索深度depth++,输出目录名后,进行递归
if (file.isDirectory()) {
//递归
transferFile(file.getCanonicalPath(), currentDepth);
} else {
if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) {
readAndWrite(file);
System.out.println(name + " has converted to utf8 ");
}
}
}
}
private static boolean isValidFile(File dirFile)throwsIOException{
if (dirFile.exists()) {
System.out.println("file exist");
return true;
}
if (dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
return true;
}
return false;
}
private static void readAndWrite(File file)throwsException{
String content = FileUtils.readFileByEncode(file.getPath(), "GBK");
FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8"));
}
public static void main(String[] args)throwsException{
//程序入口,制定src的path
String path = "/Users/mac/Downloads/unit06_jdbc/src";
transferFile(path, 1);
}
}
public class FileUtils{
public static void writeByBufferedReader(String path, String content){
try {
File file = new File(path);
file.delete();
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public staticStringreadFileByEncode(String path, String chatSet)throwsException{
InputStream input = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(input, chatSet);
BufferedReader reader = new BufferedReader(in);
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
while (line != null) {
sb.append(line);
sb.append("\r\n");
line = reader.readLine();
}
return sb.toString();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有