Browse Source

青涩知夏->优化文件写入内容

青涩知夏 4 years ago
parent
commit
6ce1281083

+ 6 - 4
src/main/java/cn/nosum/common/util/NettyFileUtil.java

@@ -14,21 +14,22 @@ public class NettyFileUtil {
     private static final Logger logger = LoggerFactory.getLogger(NettyFileUtil.class);
 
     private final static Integer BYTE_BUFFER_LENGTH=1024;
+    private final static String LINE_SEPARATOR= System.getProperty("line.separator");
 
     /**
      * 将内存中的数据写入到磁盘,如果路径不存在则创建
      * @param data 文件内容
      * @param filePath 文件保存路劲
+     * @param append 文件存在时是否追加内容
      */
-    public static void dataToFile(String data, String filePath) {
-        System.err.println(filePath);
+    public static void dataToFile(String data, String filePath,boolean append) {
         File file = new File(filePath);
         if (!file.getParentFile().exists()) {
             file.getParentFile().mkdirs();
         }
-        try (FileOutputStream fileOutputStream = new FileOutputStream(file);
+        try (FileOutputStream fileOutputStream = new FileOutputStream(file,append);
              FileChannel fileChannel = fileOutputStream.getChannel()) {
-            ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_LENGTH);
+            ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_LENGTH+LINE_SEPARATOR.length());
             int i = 0;
             int length = data.getBytes().length;
             // 一次性读取完毕
@@ -45,6 +46,7 @@ public class NettyFileUtil {
                     fileChannel.write(byteBuffer);
                 }
             }
+            fileOutputStream.write(LINE_SEPARATOR.getBytes());
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 1 - 1
src/main/java/cn/nosum/gateway/slot/FileProcessorSlotChain.java

@@ -11,7 +11,7 @@ import com.alibaba.fastjson.JSON;
 public class FileProcessorSlotChain extends AbstractLinkedProcessorSlot<Context> {
     @Override
     public void exec(Context context) throws Throwable {
-        NettyFileUtil.dataToFile(JSON.toJSONString(context.getRequest().getParameters()),context.getRequest().getUrl());
+        NettyFileUtil.dataToFile(JSON.toJSONString(context.getRequest().getParameters()), context.getRequest().getUrl(), true);
         fireExec(context);
     }
 }