1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.rolling.action;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.nio.channels.FileChannel;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26
27
28
29
30 public class FileRenameAction extends AbstractAction {
31
32
33
34
35 private final File source;
36
37
38
39
40 private final File destination;
41
42
43
44
45 private final boolean renameEmptyFiles;
46
47
48
49
50
51
52
53
54 public FileRenameAction(final File src, final File dst, final boolean renameEmptyFiles) {
55 source = src;
56 destination = dst;
57 this.renameEmptyFiles = renameEmptyFiles;
58 }
59
60
61
62
63
64
65 @Override
66 public boolean execute() {
67 return execute(source, destination, renameEmptyFiles);
68 }
69
70
71
72
73
74
75 public File getDestination() {
76 return this.destination;
77 }
78
79
80
81
82
83
84 public File getSource() {
85 return this.source;
86 }
87
88
89
90
91
92
93 public boolean isRenameEmptyFiles() {
94 return renameEmptyFiles;
95 }
96
97
98
99
100
101
102
103
104
105 public static boolean execute(final File source, final File destination, final boolean renameEmptyFiles) {
106 if (renameEmptyFiles || source.length() > 0) {
107 final File parent = destination.getParentFile();
108 if (parent != null && !parent.exists()) {
109
110
111
112 parent.mkdirs();
113 if (!parent.exists()) {
114 LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
115 return false;
116 }
117 }
118 final Path sourcePath = source.toPath();
119 try {
120 Files.move(sourcePath, destination.toPath());
121 return true;
122 } catch (final Exception ex) {
123 LOGGER.error("Unable to rename {} to {} due to {} - {}", source.toString(), destination.toString(),
124 ex.getClass().getSimpleName(), ex.getMessage());
125 try {
126
127 Files.copy(sourcePath, destination.toPath());
128 Files.delete(sourcePath);
129 return true;
130 } catch (final Exception iex) {
131 LOGGER.error("Unable to rename file {} to {} due to {} - {}", source.getAbsolutePath(),
132 destination.getAbsolutePath(), iex.getClass().getSimpleName(), iex.getMessage());
133 }
134 }
135 } else {
136 try {
137 Files.delete(source.toPath());
138 } catch (final Exception ex) {
139 LOGGER.error("Unable to delete empty file " + source.getAbsolutePath());
140 }
141 }
142
143 return false;
144 }
145
146 @Override
147 public String toString() {
148 return FileRenameAction.class.getSimpleName() + '[' + source + " to " + destination
149 + ", renameEmptyFiles=" + renameEmptyFiles + ']';
150 }
151
152 }