入题所述,
解决方案 »
- 刚开始学java,写出了一个小程序,高兴一下。(求小于输入数的质数)嘿嘿!
- java问题,求高手解决~~~~
- 请教一个集合的设计,用来存储一堆书籍对象,实现针对书名快速查询
- 各位帮着看一段怪异的代码(只有几行,但写法怪异).是servlet的源码里的.
- 请问如何在JInternalFrame中添加一个JDialog
- Applet用microsoft sdk for java编译通过,但repaint 不能触发 paint,是编译器问题吗?
- 这个方法有什么功能?
- ******** 最近工作挺闲的,想揽话干 *********
- 有汉化了的JDK吗?
- 没人帮忙吗?我想用JAVA编一个像WINDOW中的画图软件,现在出现了如下问题,请各位老哥老姐帮帮忙!
- 我用jbuild生成了exe文件的问题???急急急急
- 电子邮件收发系统的界面设计中遇到的困惑(类似outlook)
可不可以将Area的内容装到一个OutputStream中?之后用这个进行输出?
如何将一个OutputStream定为默认输出流,等待高手
final int BUFFER_LENGTH = 1024;
byte buf[] = new byte[BUFFER_LENGTH];
int pos = 0;
public void write(int b) throws IOException {
buf[pos ++] = (byte)b;
if (pos >= BUFFER_LENGTH)
flush();
}
public void flush() throws IOException
{
if (pos >= BUFFER_LENGTH)
txtLog.append(new String(buf));
else
txtLog.append(new String(buf, 0, pos));
pos = 0;
}
};
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
//output message to textarea
System.setOut(new PrintStream(jtextOut, true));
final int LENGTH = 256;
byte[] bb = new byte[LENGTH];
int p = 0;
public void write(int b) throws IOException {
bb[p++] = (byte)b;
if(p >= LENGTH){
flush();
}
}
public void flush()throws IOException{
String str = new String(bb,0,p);
jTextArea1.append(str);
p = 0;
}
},true);
System.setOut(ps);
System.out.println("test str"};
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.event.*;class MyPipe implements Runnable{
private JTextArea textarea;
private PipedInputStream pis = new PipedInputStream();
private PipedOutputStream pos;
private BufferedReader reader = new BufferedReader(
new InputStreamReader(pis));
private Thread thread = null;
public MyPipe(JTextArea ta) throws IOException{
textarea = ta;
pos = new PipedOutputStream(pis);
thread = new Thread(this);
thread.start();
}
public PipedOutputStream getOut(){
return pos;
}
public void run(){
String line = null;
while(thread == Thread.currentThread()){
try{
line = reader.readLine();
}
catch(IOException ioe){
break;
}
if(line == null){
break;
}
else{
final String s = line;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
textarea.append(s);
textarea.append(System.getProperty("line.separator", "\n\r"));
}
});
}
}
}
public void close(){
thread = null;
}
}public class Sink extends JFrame{
private MyPipe pipe;
public Sink() throws IOException{
JTextArea area = new JTextArea(10, 40);
area.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println(e.getX() + ", " + e.getY());
}
});
JScrollPane scroll = new JScrollPane(area);
pipe = new MyPipe(area);
System.setOut(new PrintStream(pipe.getOut(), true));
this.getContentPane().add(scroll, BorderLayout.CENTER);
this.pack();
this.setLocation(200, 200);
this.setVisible(true);
}
public void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID() == e.WINDOW_CLOSING){
pipe.close();
System.exit(0);
}
}
public static void main(String[] args)throws IOException{
Sink sink = new Sink();
}
}
1 每次write之后直接flush,而不要等到buffer满了之后
2 OutputStream的几个write方法都应该覆盖