[color=#FF0000]输出时 报异常  输出: java.io.IOException: Stream closed [/color]package IODemo3;
/**
 * 
 * 功能:简易记事本
 *
 */
import javax.swing.*;import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Notepad extends JFrame implements ActionListener{

public static void main(String[] args) {
// TODO Auto-generated method stub Notepad t=new Notepad();
}
//定义
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;


public Notepad()
 {
 jta=new JTextArea(); 
 jmb=new JMenuBar();
 jm1=new JMenu("文件");
 jm1.setMnemonic('o');
 jmi1=new JMenuItem("打开");
 jmi2=new JMenuItem("保存");
 
 //加入
 jm1.add(jmi1);
 jm1.add(jmi2);
 jmb.add(jm1);
 //时间监听
 jmi1.addActionListener(this);
 jmi1.setActionCommand("open");
 jmi2.addActionListener(this);
 jmi2.setActionCommand("save");
 //加入面板
 
 this.add(jta);
 this.setJMenuBar(jmb);
 
 this.setLocation(300,300);
 this.setVisible(true);
 this.setSize(400,300);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 } //@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("open"))
{
//JFileChooser
JFileChooser jfc1=new JFileChooser();
jfc1.setDialogTitle("请选择文件");
jfc1.showOpenDialog(null);
jfc1.setVisible(true);

//得到路径
String filename=jfc1.getSelectedFile().getAbsolutePath();
//System.out.println("路径名为"+filename);
FileReader fr=null;
BufferedReader br=null;
try {
     fr=new FileReader(filename);
 br=new BufferedReader(fr);
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
jta.setText(allCon);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try {
fr.close();
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

}else if(e.getActionCommand().equals("save"))
{
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为");
jfc.showSaveDialog(null);
jfc.setVisible(true);

//得到希望保存的地方
String file=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;

try {
fw=new FileWriter(file);
bw=new BufferedWriter(fw);
//byte []bytes=new byte[1024];
// int n=0;
// while((n=this.jta.getText().length())!=-1)
// {
// //String s=new String(bytes ,0,n);
//System.out.println(s);

bw.write(this.jta.getText());

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try {
fw.close();
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}}
输出: java.io.IOException: Stream closed

解决方案 »

  1.   

    try {
    fw.close();
    bw.close();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    在你的代码最后的这部分 有问题 应该先关闭bw 再关闭fw
    try {bw.close();
    fw.close();
    } catch (IOException e1) {
    这样就没问题了 不会有异常
      

  2.   

    1).在保存操作时bw.write(this.jta.getText());语句之后添加语句bw.flush();即可,解决你的Stream closed异常问题,缓冲区操作时基本和flush操作是分不开的。至于为什么要flush,则是防止数据的纰漏!
    2).你的代码写的相当不整齐
    3).你在部分finally块中仍然使用去写try {
    fw.close();
    bw.close();
    } catch (IOException e1) {

    e1.printStackTrace();
    }
    是一种错误的异常处理方式,以后尽量避免!!
      

  3.   

    bw.write(this.jta.getText());
    输出时怎么换行?
    谢谢。
      

  4.   

    受教了   我搞了好一会  还是baidu 好