Browse Source

增加关闭流

luxueshi 3 years ago
parent
commit
b1082bd6f1
2 changed files with 20 additions and 4 deletions
  1. 1 1
      src/main/java/Test.java
  2. 19 3
      src/main/java/util/CompareFileUtil.java

+ 1 - 1
src/main/java/Test.java

@@ -17,7 +17,7 @@ public class Test {
      * @param args
      * @param args
      */
      */
     public static void main(String[] args) {
     public static void main(String[] args) {
-        String path1 = "D:\\data\\D-physical_world";
+        String path1 = "D:\\aaaa.txt";
         String path2 = "D:\\data\\test\\D-physical_world";
         String path2 = "D:\\data\\test\\D-physical_world";
         TestThread testThread = new TestThread(path1, path2);
         TestThread testThread = new TestThread(path1, path2);
         testThread.start();
         testThread.start();

+ 19 - 3
src/main/java/util/CompareFileUtil.java

@@ -70,10 +70,11 @@ public class CompareFileUtil {
     }
     }
 
 
     public static String getFileMD5String(File file) {
     public static String getFileMD5String(File file) {
+        FileInputStream in = null;
+        FileChannel ch = null;
         try {
         try {
-
-            FileInputStream in = new FileInputStream(file);
-            FileChannel ch = in.getChannel();
+            in = new FileInputStream(file);
+            ch = in.getChannel();
 
 
             //700000000 bytes are about 670M
             //700000000 bytes are about 670M
             int maxSize=700000000;
             int maxSize=700000000;
@@ -105,6 +106,21 @@ public class CompareFileUtil {
 
 
         }catch (Exception e) {
         }catch (Exception e) {
             System.out.println(e.getMessage());
             System.out.println(e.getMessage());
+        }finally{
+            if (null != ch) {
+                try {
+                    ch.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != in) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
         }
         return null;
         return null;
     }
     }