观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  422 随笔 :: 0 文章 :: 86 评论 :: 139万 阅读
复制代码
package com.function;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.ui.Tools_UiDataPanelJTextArea;

/*
 * 将System.out.println() 控制台信息重新导向
 */
public class Tools_DataRedirection extends OutputStream   {
    private final JTextArea destination;  
    public Tools_DataRedirection (JTextArea destination)  
    {  
        if (destination == null)  
            throw new IllegalArgumentException ("Destination is null");  
  
  
        this.destination = destination;  
    }  
    @Override  
    public void write(byte[] buffer, int offset, int length) throws IOException  
    {  
        final String text = new String (buffer, offset, length);  
        SwingUtilities.invokeLater(new Runnable ()  
            {  
                @Override  
                public void run()   
                {  
                    destination.append (text);  
                }  
            });  
    }  
    @Override  
    public void write(int b) throws IOException  
    {  
        write (new byte [] {(byte)b}, 0, 1);  
    }  

}
复制代码

 

 
复制代码
package com.ui;

import java.io.PrintStream;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.function.Tools_DataRedirection;
/*
 * 数据显示文本域类
 */
public class Tools_UiDataPanelJTextArea extends JTextArea {
    public Tools_UiDataPanelJTextArea() {
        // TODO Auto-generated constructor stub
        Tools_DataRedirection out = new Tools_DataRedirection (this);  
        System.setOut (new PrintStream (out));//设置输出重定向   
        System.setErr(new PrintStream(out));//将错误输出也重定向,用于e.pritnStackTrace 

        
    }

}
复制代码

 

 



posted on   观心静  阅读(294)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示