import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class rr implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{ m.setSize(300,166);
m.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
} public void actionPerformed(ActionEvent e)
{
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} l.setText(new String(b,0,i));
}
public static void main(String []args)
{
rr r=new rr();
r.go();
} } 我这个运行后为什么第一次点按钮没什么问题,再点一次好像出现了异常了
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at rr.actionPerformed(rr.java:37)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
而且l里的文字也不见了,还有每次l里面的文字都是只显示一点点,如果我把窗口拉大或拉小文字才会变长
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class rr implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{ m.setSize(300,166);
m.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
} public void actionPerformed(ActionEvent e)
{
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} l.setText(new String(b,0,i));
}
public static void main(String []args)
{
rr r=new rr();
r.go();
} } 我这个运行后为什么第一次点按钮没什么问题,再点一次好像出现了异常了
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at rr.actionPerformed(rr.java:37)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
而且l里的文字也不见了,还有每次l里面的文字都是只显示一点点,如果我把窗口拉大或拉小文字才会变长
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} 你第一次点的时候已经把out给close掉了,你再点,它还去哪找out啊???
{
里面在out=new FileInputStream(f);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{
m.setSize(800,666);
m.setLayout(new FlowLayout());
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
} public void actionPerformed(ActionEvent e)
{
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
}
l.setText(new String(b,0,i));
l.setSize(300,30);
}
public static void main(String []args)
{
Test t=new Test();
t.go();
} }