Sina.iRoid
جمعه 10 بهمن 1393, 10:19 صبح
سلام
من تعدای فایل عکس دارم و می خوام همه اون عکس ها رو بدون اینکه نام اصلیشون عوش بشه، فقط ابتداش یه عبارتی و اضافه کنم. مشکل اینجاست که در کدی که من نوشتم اسم فایل ها عوض نمیشه و فقط اسم دایرکتوری عوض میشه. ممنون میشم اگر کمک کنید.
import java.io.File;
public class MainClass {
public static void main(String[] args) {
String filePath01 = "C:\\Users\\SIna\\Videos\\s";
File file01 = new File(filePath01);
// get list of name file
String[] file01Name = file01.list();
// Convert string to lowerCase
for (int i = 0; i < file01Name.length; i++) {
file01Name[i] = file01Name[i].toLowerCase();
}
for (int i = 0; i < file01Name.length; i++) {
String filePath02 = "C:\\Users\\SIna\\Videos\\new\\" + file01Name[i];
file01.renameTo(new File(filePath02 + "android" + file01Name[i]));
}
}
}
ahmad.mo74
جمعه 10 بهمن 1393, 21:39 عصر
package com.util.file;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author avb
*/
public final class FileUtil {
public static boolean renameFiles(String dir, String newName, String prefix, String suffix) throws IOException {
File root = Paths.get(dir).toFile();
if (!root.isDirectory()) {
return false;
}
File[] files = root.listFiles();
if (files != null) {
List<File> fileList = Arrays.asList(files).stream().filter(file -> !file.isDirectory()).collect(Collectors.toList());
if (fileList.isEmpty()) {
return true;
}
prefix = prefix == null ? "" : prefix;
suffix = suffix == null ? "" : suffix;
String path = root.getCanonicalPath();
if (fileList.size() == 1) {
File file = fileList.get(0);
return file.renameTo(new File(path + File.separator + prefix
+ (newName == null ? file.getName() : newName) + suffix));
}
int c = 1;
for (File file : fileList) {
String fileName = newName == null ? file.getName() : newName;
int index = fileName.lastIndexOf('.');
StringBuilder sb = new StringBuilder();
sb.append(path).append(File.separator).append(pref ix);
if (index > 0) {
sb.append(fileName.substring(0, index)).append(" (").append(c++).append(")").append(fileName.substring(index));
} else {
sb.append(fileName).append(" (").append(c++).append(")");
}
sb.append(suffix);
if (!file.renameTo(new File(String.valueOf(sb)))) {
return false;
}
}
}
return true;
}
public static boolean markFiles(String dir, int from, String separator, boolean removeFileName) throws IOException {
File root = Paths.get(dir).toFile();
if (!root.isDirectory()) {
return false;
}
File[] files = root.listFiles();
if (files != null) {
List<File> fileList = Arrays.asList(files).stream().filter(file -> !file.isDirectory()).collect(Collectors.toList());
if (fileList.isEmpty()) {
return true;
}
separator = separator == null ? " - " : separator;
String path = root.getCanonicalPath();
for (File file : fileList) {
if (!file.renameTo(new File(path
+ File.separator
+ String.valueOf(from++)
+ (removeFileName ? getExtension(file, "") : separator + file.getName())))) {
return false;
}
}
}
return true;
}
public static String getExtension(File file, String def) {
if (file.isFile()) {
String fileName = file.getName();
int index = fileName.lastIndexOf('.');
if (index > 0) {
return fileName.substring(index);
}
return def;
}
return null;
}
public static void main(String[] args) throws IOException {
System.out.println(renameFiles("D:/test", "file", null, ".png"));
System.out.println(markFiles("D:/test", 1, null, true));
}
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.