Stream closed问题,IO编程,帮我看看!
package com.Textbook;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class TextBook extends JFrame implements ActionListener{
JTextArea jt=null;
JMenu jm1=null;
JMenuBar jmb=null;
JMenuItem jmi1,jmi2=null;
public static void main(String args[])
{
TextBook tb=new TextBook();
}
public TextBook()
{
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
jm1.setMnemonic('F');
jmi1=new JMenuItem("打开");
jmi2=new JMenuItem("保存");
//加入
jm1.add(jmi1);jm1.add(jmi2);
jmb.add(jm1);
this.setJMenuBar(jmb);
//注册监听
jmi1.addActionListener(this);
jmi1.setActionCommand("摁下打开");
jmi2.addActionListener(this);
jmi2.setActionCommand("摁下保存");
//把jm1放入jmb,jmb是菜单条
//把ITEM加入菜单
jt=new JTextArea();
this.add(jt);
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
File buffile=null;
// TODO Auto-generated method stub
if(e.getActionCommand().equals("摁下打开"))
{
this.openFile(this.jt);
}
if(e.getActionCommand().equals("摁下保存"))
{
this.saveFile(this.jt);
}
}
private void saveNewFile() {
// TODO Auto-generated method stub
}
private void saveFile(JTextArea jt) {
// TODO Auto-generated method stub
// System.out.println("执行保存");
// JFileChooser jfc=new JFileChooser();
// jfc.setVisible(true);
// jfc.setDialogTitle("保存");
// jfc.showOpenDialog(null);
// File f0=jfc.getSelectedFile();
// System.out.println(f0.getAbsolutePath());
// FileWriter fw=null;
// BufferedWriter bw=null;
// try{
// fw=new FileWriter(buffile);
// bw=new BufferedWriter(fw);
// String alls=jta.getText();
// bw.write(alls);
// System.out.println("写入保存成功");
//
// }catch(Exception e1){}finally{try {
// fw.close();bw.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }}
JFileChooser jfc2=new JFileChooser();
jfc2.setDialogTitle("保存");
jfc2.showSaveDialog(null);
jfc2.setVisible(true);
String f0=jfc2.getSelectedFile().getAbsolutePath();
FileWriter fw2=null;
BufferedWriter bw2=null;
try{
fw2=new FileWriter(f0);
bw2=new BufferedWriter(fw2);
bw2.write(jt.getText());
System.out.println(jt.getText());
System.out.println(jfc2.getSelectedFile().getAbsolutePath());
}catch(Exception e1)
{e1.printStackTrace();}
finally{
try {
fw2.close();
bw2.close();
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}}
}
private void openFile(JTextArea jta) {
// TODO Auto-generated method stub
System.out.println("执行打开");
JFileChooser jfc1=new JFileChooser();
//设置名字
jfc1.setDialogTitle(null);
//设置打开格式为默认
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
File f1=jfc1.getSelectedFile();
System.out.println(f1.getAbsolutePath());
FileReader fr1=null;
BufferedReader bfr1=null;
try{
fr1=new FileReader(f1.getAbsolutePath());
bfr1=new BufferedReader(fr1);
String s="";
String alls="";
while((s=bfr1.readLine())!=null)
{
alls+=s+"\r\n";
}
jta.setText(alls);
}catch(Exception e){}finally{
try {
fr1.close();
bfr1.close();
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}}
}
}
/*报错内容
java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:26)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:99)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.BufferedWriter.close(BufferedWriter.java:246)
at com.Textbook.TextBook.saveFile(TextBook.java:115)
at com.Textbook.TextBook.actionPerformed(TextBook.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
*/