|
@@ -57,6 +57,7 @@ public class FileManager {
|
|
|
* @param newPath 目标文件夹路径
|
|
|
*/
|
|
|
public boolean moveFolder(String oldPath, String newPath) {
|
|
|
+ boolean result = true;
|
|
|
File sourceFolder = new File(oldPath);
|
|
|
//把源文件转移到目标文件夹下
|
|
|
File targetFolder = new File(newPath, sourceFolder.getName());
|
|
@@ -65,36 +66,52 @@ public class FileManager {
|
|
|
}
|
|
|
if (sourceFolder.isDirectory()) {
|
|
|
if (!targetFolder.exists()) {
|
|
|
- targetFolder.mkdir();
|
|
|
+ result = targetFolder.mkdir();
|
|
|
}
|
|
|
File[] files = sourceFolder.listFiles();
|
|
|
if (files != null) {
|
|
|
for (File file : files) {
|
|
|
if ( file.isFile() ) {
|
|
|
FileChannel sourceChannel = null;
|
|
|
+ FileInputStream fileInputStream = null;
|
|
|
+ FileOutputStream fileOutputStream = null;
|
|
|
FileChannel destChannel = null;
|
|
|
try {
|
|
|
- sourceChannel = new FileInputStream(file).getChannel();
|
|
|
- destChannel = new FileOutputStream(new File(targetFolder.getPath(), file.getName())).getChannel();
|
|
|
+ fileInputStream = new FileInputStream(file);
|
|
|
+ fileOutputStream = new FileOutputStream(new File(targetFolder.getPath(), file.getName()));
|
|
|
+ sourceChannel = fileInputStream.getChannel();
|
|
|
+ destChannel = fileOutputStream.getChannel();
|
|
|
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
|
|
|
sourceChannel.close();
|
|
|
- file.delete();
|
|
|
- } catch (IOException e) {}finally {
|
|
|
+ result = file.delete();
|
|
|
+ } catch (IOException e) {
|
|
|
+ result = false;
|
|
|
+ }finally {
|
|
|
try {
|
|
|
- if (Objects.nonNull(sourceChannel))
|
|
|
+ if (Objects.nonNull(sourceChannel)) {
|
|
|
sourceChannel.close();
|
|
|
- if (Objects.nonNull(destChannel))
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(destChannel)) {
|
|
|
destChannel.close();
|
|
|
- } catch (IOException e) {}
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(fileInputStream)) {
|
|
|
+ fileInputStream.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(fileOutputStream)) {
|
|
|
+ fileOutputStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ result = false;
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
moveFolder(file.getPath(), new File(targetFolder.getPath(), file.getName()).getPath());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- sourceFolder.delete();
|
|
|
+ result = sourceFolder.delete();
|
|
|
}
|
|
|
- return true;
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -156,23 +173,22 @@ public class FileManager {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean deleteFolder(String folderPath){
|
|
|
+ boolean ret = true;
|
|
|
File fileFolder = new File(folderPath);
|
|
|
if ( fileFolder != null && fileFolder.exists() ){
|
|
|
if ( fileFolder.isDirectory() ){
|
|
|
File[] files = fileFolder.listFiles();
|
|
|
for (File file : files) {
|
|
|
if ( file.isDirectory() ){
|
|
|
- deleteFolder(file.getAbsolutePath());
|
|
|
+ ret = deleteFolder(file.getAbsolutePath());
|
|
|
} else {
|
|
|
- file.delete();
|
|
|
+ ret = file.delete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- fileFolder.delete();
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+ ret = fileFolder.delete();
|
|
|
}
|
|
|
- return true;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -333,7 +349,7 @@ public class FileManager {
|
|
|
if (replaceFile) {
|
|
|
Files.move(srcFile, destFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
} else {
|
|
|
- Files.move(srcFile, destFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ Files.move(srcFile, destFile, StandardCopyOption.COPY_ATTRIBUTES);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
return false;
|