普通网友 2016-08-22 02:58 采纳率: 100%
浏览 6577
已采纳

java多线程报错 java.io.IOException: Stream closed

虽然程序可以正确的讲读取到的信息输出到文本文件 但是报错:java.io.IOException: Stream closed
不知道是什么原因 菜鸟请各位帮忙看一下
代码如下:
package test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test implements Runnable{
String lineString;
public Test(String line){
this.lineString = line;
}
static FileWriter writer;
static FileReader reader;
public static void main(String[] args) throws IOException {
try {
reader = new FileReader("/Users/tcredit-0002/Desktop/car.txt");
Scanner scanner = new Scanner(reader);
String lineString;
writer = new FileWriter("/Users/tcredit-0002/Desktop/hebei.txt");
// TODO Auto-generated method stub
ExecutorService pool = Executors.newFixedThreadPool(3);
while(scanner.hasNext()){
lineString = scanner.nextLine();
pool.execute(new Test(lineString));
}
scanner.close();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void run() {
    try {
        writeResult(lineString);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static synchronized void writeResult(String lineString) throws InterruptedException{
    try {
        writer.write(lineString+"\r\n");
        writer.flush();
        System.out.println(lineString);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

  • 写回答

3条回答 默认 最新

  • yuyubingMan 2016-08-22 03:06
    关注

    IO流被提前关闭了,你把关闭IO流放到finally里面就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿