public void write(String filepath){
try{
String encoding = "GBK";
File f=new File(filepath);
if(f.isFile()&&f.exists()){
BufferedWriter bw=new BufferedWriter(new FileWriter(f,false));
for(int i=0;i<commoditylist.size();i++){
bw.write(getInfo(commoditylist.get(i))+"\r\n");
bw.close();
}
}else{
f.createNewFile();
}
}catch(Exception e){
e.printStackTrace();
}
}
这是代码,哪里有问题了呀
下面是报错信息:
java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at CommodityManage.write(CommodityManage.java:86)
at CommodityManage.add(CommodityManage.java:25)
at Command.analyse(Command.java:14)
at Server.main(Server.java:24)
然后我同学告诉我原因是:bw.close()调用不应该在for循环中;
不明白为什么这个地方不能用bw.close()调用语句呢????? |
|