-
Notifications
You must be signed in to change notification settings - Fork 8
/
PicRename.java
51 lines (44 loc) · 1.62 KB
/
PicRename.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.concurrent.atomic.LongAdder;
/**
* @author: decaywood
* @date: 2016/1/16 9:23.
*/
public class PicRename {
private static final LongAdder longAdder = new LongAdder();
private static long increament() {
longAdder.increment();
return longAdder.intValue();
}
private static void reset() {
longAdder.reset();
}
private synchronized static void nioTransferCopy(File source, File target) {
try (FileInputStream inStream = new FileInputStream(source);
FileOutputStream outStream = new FileOutputStream(target);
FileChannel in = inStream.getChannel();
FileChannel out = outStream.getChannel()) {
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = PicRename.class.getClassLoader().getResource(".").getPath();
File file = new File(path);
String split = File.separator;
File folder = new File(file.getPath().trim() + split + "changed");
if(!folder.exists()) folder.mkdir();
String outputPath = folder.getPath() + split;
Arrays.stream(file.listFiles())
.parallel()
.filter(x -> x.isFile() && x.getName().contains(".jpg"))
.forEach(x -> nioTransferCopy(x, new File(outputPath + increament() + ".jpg")));
reset();
}
}