|
@@ -1,9 +1,6 @@
|
|
|
package com.tidecloud.dataacceptance.service.impl;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.nio.ByteBuffer;
|
|
|
import java.nio.CharBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
@@ -298,34 +295,26 @@ public class WatchServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
|
|
|
public static void writeDevice2File(File file, String deviceStr){
|
|
|
-
|
|
|
-
|
|
|
- Integer length = deviceStr.getBytes().length;
|
|
|
+ BufferedWriter bufferedWriter = null;
|
|
|
try {
|
|
|
- OutputStream outputStream = new FileOutputStream(file, true);
|
|
|
- byte[] lengthBytes = int2bytes(length);
|
|
|
- byte[] deviceBytes = deviceStr.getBytes();
|
|
|
- byte[] dataBytes = ArrayUtils.addAll(lengthBytes, deviceBytes);
|
|
|
- outputStream.write(dataBytes);
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
+ bufferedWriter = new BufferedWriter(new FileWriter(file, true));
|
|
|
+ bufferedWriter.write(deviceStr);
|
|
|
+ bufferedWriter.newLine();
|
|
|
+ bufferedWriter.flush();
|
|
|
} catch (IOException e) {
|
|
|
logger.error(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (bufferedWriter != null) {
|
|
|
+ try {
|
|
|
+ bufferedWriter.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("close file[{}] failed: {}", file.getName(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
-// public void writeDevice2File(File file, String deviceStr){
|
|
|
-// Integer length = deviceStr.getBytes().length;
|
|
|
-// try {
|
|
|
-// OutputStream outputStream = new FileOutputStream(file, true);
|
|
|
-// outputStream.write(int2bytes(length));;
|
|
|
-// outputStream.write(deviceStr.getBytes());
|
|
|
-// outputStream.flush();
|
|
|
-// outputStream.close();
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
cause.printStackTrace();
|