杨先森的博客 2020-05-26 19:38 采纳率: 50%
浏览 1109
已结题

写文件的时候报java.io.IOException: Stream closed

问题:
在写文件的时候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);
      }
    }
  }
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-27 00:12
    关注

    看下具体报错在哪里,你的代码存在调用了 bw.close() 之后又调用写入的逻辑

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)