需求如下:
一个文件夹有很多图片,图片名字混杂,要求把当前文件夹所有的文件复制到另一个文件目录下,但是名字要从从1开始,按照时间或大小或者名称
先后排序
code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created by simon on 16-12-19.
*/
@SuppressWarnings("Duplicates")
public class Demo {
public static void main(String ss[]) {
File file = new File("/home/simon/桌面/我们的故事/img");
orderByDate("/home/simon/桌面/我们的故事/img");
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
File temp = tempList[i];
copyFile(temp.getPath(), "/home/simon/桌面/我们的故事/gen/" + (i + 1) + getFileSux(temp));
}
}
//获取文件后缀
public static String getFileSux(File f) {
String fileName = f.getName();
String prefix = fileName.substring(fileName.lastIndexOf("."));
return prefix;
}
//复制文件
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//重命名
public static void renameFile(String path, String oldname, String newname) {
if (!oldname.equals(newname)) {//新的文件名和以前文件名不同时,才有必要进行重命名
File oldfile = new File(path + "/" + oldname);
File newfile = new File(path + "/" + newname);
if (!oldfile.exists()) {
return;//重命名文件不存在
}
if (newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
System.out.println(newname + "已经存在!");
else {
oldfile.renameTo(newfile);
}
} else {
System.out.println("新文件名和旧文件名相同...");
}
}
//按照文件大小排序
public static void orderByLength(String fliePath) {
List<File> files = Arrays.asList(new File(fliePath).listFiles());
Collections.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.length() - f2.length();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj) {
return true;
}
});
for (File f : files) {
if (f.isDirectory()) continue;
System.out.println(f.getName() + ":" + f.length());
}
}
//按照文件名称排序
public static void orderByName(String fliePath) {
List files = Arrays.asList(new File(fliePath).listFiles());
Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isFile())
return -1;
if (o1.isFile() && o2.isDirectory())
return 1;
return o1.getName().compareTo(o2.getName());
}
});
}
//按日期排序
public static void orderByDate(String fliePath) {
File file = new File(fliePath);
File[] fs = file.listFiles();
Arrays.sort(fs, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.lastModified() - f2.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
public boolean equals(Object obj) {
return true;
}
});
}
}