|
@@ -23,8 +23,9 @@ public class FileManager {
|
|
|
public Double getFolderSize(String sizePath) {
|
|
|
Double size = 0.0;
|
|
|
Path folderPath = Paths.get(sizePath);
|
|
|
+ DirectoryStream<Path> stream = null;
|
|
|
try {
|
|
|
- DirectoryStream<Path> stream = Files.newDirectoryStream(folderPath);
|
|
|
+ stream = Files.newDirectoryStream(folderPath);
|
|
|
for (Path path : stream) {
|
|
|
if (Files.isRegularFile(path)) {
|
|
|
size += Files.size(path);
|
|
@@ -32,7 +33,11 @@ public class FileManager {
|
|
|
size += getFolderSize(path.toFile().getAbsolutePath());
|
|
|
}
|
|
|
}
|
|
|
- } catch (IOException e) {}
|
|
|
+ } catch (IOException e) {}finally {
|
|
|
+ try {
|
|
|
+ stream.close();
|
|
|
+ } catch (IOException e) {}
|
|
|
+ }
|
|
|
if ( size > 0 ) {
|
|
|
size = size / 1024 / 1024 ;
|
|
|
DecimalFormat df = new DecimalFormat("#.####");
|
|
@@ -63,14 +68,19 @@ public class FileManager {
|
|
|
if (files != null) {
|
|
|
for (File file : files) {
|
|
|
if ( file.isFile() ) {
|
|
|
+ FileChannel sourceChannel = null;
|
|
|
+ FileChannel destChannel = null;
|
|
|
try {
|
|
|
- FileChannel sourceChannel = new FileInputStream(file).getChannel();
|
|
|
- FileChannel destChannel = new FileOutputStream(new File(targetFolder.getPath(), file.getName())).getChannel();
|
|
|
+ sourceChannel = new FileInputStream(file).getChannel();
|
|
|
+ destChannel = new FileOutputStream(new File(targetFolder.getPath(), file.getName())).getChannel();
|
|
|
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
|
|
|
sourceChannel.close();
|
|
|
file.delete();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ } catch (IOException e) {}finally {
|
|
|
+ try {
|
|
|
+ sourceChannel.close();
|
|
|
+ destChannel.close();
|
|
|
+ } catch (IOException e) {}
|
|
|
}
|
|
|
} else {
|
|
|
moveFolder(file.getPath(), new File(targetFolder.getPath(), file.getName()).getPath());
|
|
@@ -342,4 +352,22 @@ public class FileManager {
|
|
|
return 0.0;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件流
|
|
|
+ * @param filePath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FileInputStream getArchiveFile(String filePath) {
|
|
|
+ FileInputStream fileOutputStream = null;
|
|
|
+ try {
|
|
|
+ fileOutputStream = new FileInputStream(filePath);
|
|
|
+ } catch (FileNotFoundException e) {}
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ fileOutputStream.close();
|
|
|
+ } catch (IOException e) {}
|
|
|
+ }
|
|
|
+ return fileOutputStream;
|
|
|
+ }
|
|
|
}
|