{"id":437,"date":"2013-05-28T15:17:18","date_gmt":"2013-05-28T15:17:18","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=437"},"modified":"2013-05-28T15:17:18","modified_gmt":"2013-05-28T15:17:18","slug":"redirect-standard-outputerror-java","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/redirect-standard-outputerror-java\/","title":{"rendered":"How To Redirect Standard Output\/Error In Java"},"content":{"rendered":"<p>System.out and System.err stream objects are mapped to \u201cstandard\u201d output and error stream respectively. By default, Java display standard output\/error on display console.<\/p>\n<p>Thus, when we print a statement using System.out:<\/p>\n<div>\n<div id=\"highlighter_800245\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>System.out.println(<\/code><code>\"Hello World!\"<\/code><code>);<\/code><\/div>\n<div><code>System.err.println(<\/code><code>\"errr.. Hello World!\"<\/code><code>);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>It prints the messages to default console.<\/p>\n<p>What if you want to reassign the \u201cstandard\u201d output and error stream? Lets say you want to redirect all those standard out messages in a File.<\/p>\n<p>System class provides some useful API to re-assign \u201cstandard\u201d input, output and error streams.<\/p>\n<ul>\n<li><code>setErr(PrintStream err)<\/code>: Reassigns the \u201cstandard\u201d error output stream<\/li>\n<li><code>setIn(InputStream in)<\/code>: Reassigns the \u201cstandard\u201d input stream.<\/li>\n<li><code>setOut(PrintStream out)<\/code>: Reassigns the \u201cstandard\u201d output stream.<\/li>\n<\/ul>\n<p>In below Java code we reassign \u201cstandard\u201d output to a file and redirect all sysout messages to that file.<\/p>\n<div>\n<div id=\"highlighter_906420\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>System.out.println(<\/code><code>\"January\"<\/code><code>);<\/code><\/div>\n<div><code>System.out.println(<\/code><code>\"February\"<\/code><code>);<\/code><\/div>\n<div><\/div>\n<div><code>PrintStream ps = <\/code><code>new<\/code> <code>PrintStream(<\/code><code>\"C:\/sample.txt\"<\/code><code>);<\/code><\/div>\n<div><code>System.setOut(ps);<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>System.out.println(<\/code><code>\"March\"<\/code><code>);<\/code><\/div>\n<div><code>System.out.println(<\/code><code>\"April\"<\/code><code>);<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>ps.close();<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Output:<\/p>\n<div>\n<div id=\"highlighter_331173\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>January<\/code><\/div>\n<div><code>February<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><em>File: sample.txt<\/em><\/p>\n<div>\n<div id=\"highlighter_689464\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>March<\/code><\/div>\n<div><code>April<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Thus only January and February will be displayed in console and March April will be printed in sample.txt file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System.out and System.err stream objects are mapped to \u201cstandard\u201d output and error stream respectively. By default, Java display standard output\/error on display console. Thus, when we print a statement using System.out: System.out.println(&#8220;Hello World!&#8221;); System.err.println(&#8220;errr.. Hello World!&#8221;); It prints the messages to default console. What if you want to reassign the \u201cstandard\u201d output and error stream? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[25],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/437"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}