Pārlūkot izejas kodu

关闭流操作

xiewenjie 3 gadi atpakaļ
vecāks
revīzija
b04e374b5f

+ 26 - 14
sxz-base/src/main/java/com/fdkk/sxz/util/CreateObjUtil.java

@@ -228,40 +228,50 @@ public class CreateObjUtil {
             System.out.println(block_customized_unit);
         } catch (IOException e) {
             e.printStackTrace();
+        } finally {
+            IoUtil.close(fis);
+            IoUtil.close(out);
         }
-        fis.close();
         return;
     }
 
     public static void convertVisionmodeldataToTxt(String srcpath, String despath) throws Exception {
+        FileInputStream fis = null;
+        ByteArrayInputStream stream = null;
+        BufferedOutputStream bos = null;
+        BufferedInputStream bis = null;
         try {
             File file = new File(srcpath);
-            FileInputStream fis = new FileInputStream(file);
+            fis = new FileInputStream(file);
 
             Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.NavigationInfo.parseFrom(fis);
 
             //PrintStream out = new PrintStream(despath);
             String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
-            ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
-            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
-            BufferedInputStream bis = new BufferedInputStream(stream);
+            stream = new ByteArrayInputStream(jsonFormat1.getBytes());
+            bos = new BufferedOutputStream(new FileOutputStream(despath));
+            bis = new BufferedInputStream(stream);
             int b = -1;
             while ((b = bis.read()) != -1) {
                 bos.write(b);
             }
-            //out.close();
-            bis.close();
-            bos.close();
-
         } catch (Exception e) {
             StringWriter trace = new StringWriter();
             e.printStackTrace(new PrintWriter(trace));
             CreateObjUtil.log.error(trace.toString());
             CreateObjUtil.log.error(srcpath + "|||" + despath + "convert失败");
+        } finally {
+            IoUtil.close(fis);
+            IoUtil.close(stream);
+            IoUtil.close(bos);
+            IoUtil.close(bis);
         }
     }
 
     public static void convertTxtToVisionmodeldata(String srcpath, String despath) throws Exception {
+        ByteArrayInputStream stream = null;
+        BufferedOutputStream bos = null;
+        BufferedInputStream bis = null;
         try {
             Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
             String jsonFormat = CreateObjUtil.readTxtFileToJson(srcpath);
@@ -269,20 +279,22 @@ public class CreateObjUtil {
             byte[] buf = builder.build().toByteArray();
 
             //把序列化后的数据写入本地磁盘
-            ByteArrayInputStream stream = new ByteArrayInputStream(buf);
-            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
-            BufferedInputStream bis = new BufferedInputStream(stream);
+            stream = new ByteArrayInputStream(buf);
+            bos = new BufferedOutputStream(new FileOutputStream(despath));
+            bis = new BufferedInputStream(stream);
             int b = -1;
             while ((b = bis.read()) != -1) {
                 bos.write(b);
             }
-            bis.close();
-            bos.close();
         } catch (Exception e) {
             StringWriter trace = new StringWriter();
             e.printStackTrace(new PrintWriter(trace));
             CreateObjUtil.log.error(trace.toString());
             CreateObjUtil.log.error(srcpath + "|||" + despath + "convert失败");
+        } finally {
+            IoUtil.close(stream);
+            IoUtil.close(bos);
+            IoUtil.close(bis);
         }
     }