Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Dividing output between a textfile and console or to a JFrame

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Dividing output between a textfile and console or to a JFrame

    I once wrote this little utility class for exactly this purpose. It is an OutputStream that will split all output across 2 different output streams.
    import java.io.IOException;
    import java.io.OutputStream;
     
    public class SplitOutputStream extends OutputStream {
     
    	private final OutputStream s1;
    	private final OutputStream s2;
     
    	public SplitOutputStream(OutputStream s1, OutputStream s2) {
    		this.s1 = s1;
    		this.s2 = s2;
    	}
     
    	public void close() throws IOException {
    		s1.close();
    		s2.close();
    	}
     
    	public void flush() throws IOException {
    		s1.flush();
    		s2.flush();
    	}
     
    	public void write(byte[] b) throws IOException {
    		s1.write(b);
    		s2.write(b);
    	}
     
    	public void write(byte[] b, int off, int len) throws IOException {
    		s1.write(b, off, len);
    		s2.write(b, off, len);
    	}
     
    	public void write(int b) throws IOException {
    		s1.write(b);
    		s2.write(b);
    	}
     
    }

    You can use it like this:
    		try {
    			FileOutputStream fileOut = new FileOutputStream("Log.txt");
    			System.setOut(new PrintStream(new SplitOutputStream(System.out, fileOut)));
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		}
    Now every call to System.out.println(...) should output to both the text file Log.txt as well as to the console (if the console output was the System.out previously).


    Edit: I also wrote this little handy class that can output to a custom JFrame:
    import java.awt.BorderLayout;
    import java.awt.Rectangle;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.IOException;
    import java.io.OutputStream;
     
    import javax.swing.JButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
    public class OutputToFrame extends OutputStream {
     
    	private final StringBuffer buffer;
    	private OutputFrame frame;
    	private Rectangle frameBounds;
     
    	public OutputToFrame() {
    		buffer = new StringBuffer();
    	}
     
    	public void write(int b) throws IOException {
    		char inAsChar = (char) b;
    		buffer.append(inAsChar);
    		if (inAsChar == '\n') {
    			flushToFrame();
    		}
    	}
     
    	public void flush() throws IOException {
    		super.flush();
    		flushToFrame();
    	}
     
    	public void close() throws IOException {
    		super.close();
    		if (frame != null) {
    			frame.dispose();
    		}
    	}
     
    	private void flushToFrame() {
    		if (frame == null) {
    			makeFrame();
    		}
    		frame.append(buffer.toString());
    		buffer.delete(0, buffer.length());
    	}
     
    	private void makeFrame() {
    		frame = new OutputFrame();
    		if (frameBounds != null) {
    			frame.setBounds(frameBounds);
    		}
    		frame.setVisible(true);
    		frame.addWindowListener(new WindowListener() {
    			public void windowOpened(WindowEvent e) {
    			}
    			public void windowIconified(WindowEvent e) {
    			}
    			public void windowDeiconified(WindowEvent e) {
    			}
    			public void windowDeactivated(WindowEvent e) {
    			}
    			public void windowClosing(WindowEvent e) {
    			}
    			public void windowClosed(WindowEvent e) {
    				frame = null;
    			}
    			public void windowActivated(WindowEvent e) {
    			}
    		});
    		frame.addComponentListener(new ComponentListener() {
    			public void componentShown(ComponentEvent e) {
    			}
    			public void componentResized(ComponentEvent e) {
    				frameBounds = frame.getBounds();
    			}
    			public void componentMoved(ComponentEvent e) {
    			}
    			public void componentHidden(ComponentEvent e) {
    			}
    		});
    	}
     
    	private static class OutputFrame extends JFrame {
     
    		static final long serialVersionUID = 1L;
     
    		final JTextArea textArea;
     
    		public OutputFrame() {
    			setTitle("Output");
    			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    			setBounds(100, 100, 450, 300);
    			JPanel contentPane = new JPanel();
    			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    			setContentPane(contentPane);
    			contentPane.setLayout(new BorderLayout(0, 0));
     
    			JPanel panel = new JPanel();
    			contentPane.add(panel, BorderLayout.SOUTH);
    			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
     
    			final JFrame self = this;
    			JButton btnClose = new JButton("Close");
    			btnClose.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					self.dispose();
    				}
    			});
    			panel.add(btnClose);
     
    			JScrollPane scrollPane = new JScrollPane();
    			contentPane.add(scrollPane, BorderLayout.CENTER);
     
    			textArea = new JTextArea();
    			scrollPane.setViewportView(textArea);
    		}
     
    		public void append(String s) {
    			textArea.append(s);
    		}
     
    	}
     
    }
    Since its a regular output stream you can use it as System.out and you can use it in combination with my SplitOutputStream. You can even have your output to file, console and to the JFrame all at once if you split it twice.

  2. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (September 22nd, 2014)


Similar Threads

  1. Creating a textfile with program output.
    By Paul5445 in forum Java Theory & Questions
    Replies: 6
    Last Post: September 22nd, 2014, 05:25 AM
  2. How to clear Java output console
    By rita khatei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2014, 03:42 PM
  3. How to make console output bold
    By ColeTrain in forum Java Theory & Questions
    Replies: 6
    Last Post: October 16th, 2012, 09:54 AM
  4. Having problems with output to console....help!
    By toppcon in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 13th, 2011, 06:38 PM
  5. Changing output from console to .html
    By RSYR in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: December 10th, 2009, 07:55 PM