问题:
在写文件的时候catch (Exception e) 有输出java.io.IOException: Stream closed at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116) ~[?:1.8.0_144] 的错误
麻烦io读取大佬解决下
代码段:
/**
* 读取对应buffer数据,输出到文件
*/
public void writeCsv(String strBufferKey) {
LinkedBlockingDeque<KeyValue> buffer = BufferUtil.bufferMdCsv.get(strBufferKey);
KeyValue ky = null;
long lLastTime = 0;
long lTime = 0;
int numFileCount = 1;
int numLine = 0;
int nCount = 0;
BufferedWriter bw = null;
while (true) {
try {
nCount++;
if (buffer.size() > 0) {
ky = buffer.take();
//是否要创建一个新文件
if (lTime == 0) {
lTime = Long.parseLong(ky.getStrKey());
//埋点步骤编号
int numStepNo = Integer.parseInt(strBufferKey.replaceAll("step", ""));
//csv文件目录
String strPath = pathname + File.separator + "data" + numStepNo;
//创建cvs目录
File file = new File(strPath);
if (!file.exists()) {
file.mkdirs();
}
String strFile = strPath + File.separator + csvFilename(numStepNo, numFileCount,
Long.parseLong(ky.getStrKey()));
//创建文件
FileOutputStream outputStream = new FileOutputStream(strFile);
OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, "GBK");
bw = new BufferedWriter(outputWriter);
//写入表头
String dataCommon = "";
String[] array = CmccDataUtils.data_common;
for (int i = 0; i < array.length; i++) {
if (i==array.length){
dataCommon = dataCommon + array[i];
}
}
//bw = new BufferedWriter(new FileWriter(strFile));
}
//写一行到文件
lLastTime = Long.parseLong(ky.getStrKey());
bw.write(ky.getStrValue());
bw.newLine();
numLine++;
//判断是否到规定的行,结束一个文件
if (numLine >= size) {
lTime = 0;
numLine = 0;
bw.flush();
bw.close();
numFileCount++;
} else if (!isSameDay(lTime, lLastTime)) {//判断是否跨天
lTime = 0;
numFileCount = 1;
bw.flush();
bw.close();
}
} else {
Thread.sleep(1000L * 3L);
}
//10秒钟刷新一次,没打开文件时不执行
if (lTime != 0) {
if (nCount > 10) {
nCount = 0;
if (bw != null) {
bw.flush();
}
}
}
} catch (Exception e) {
log.error("exception", e);
}
}
}