How could Java classes direct program messages to the
system console, but error messages, say to a file?

Answers were Sorted based on User's Feedback



How could Java classes direct program messages to the system console, but error messages, say to a..

Answer / munna

import java.io.*;
class Err
{
public static void main(String[] args) throws Exception
{
PrintStream ps=new PrintStream(new
FileOutputStream("output.txt"));
System.setErr(ps);
System.setOut(ps);
System.out.println("fdffdfdffffff........");
}
}

Is This Answer Correct ?    8 Yes 0 No

How could Java classes direct program messages to the system console, but error messages, say to a..

Answer / biru3121

The class System has a variable out that represents the
standard output, and the variable err that represents the
standard error device. By default, they both point at the
system console. This how the standard output could be re-
directed:

Stream st = new Stream(new FileOutputStream("output.txt"));
System.setErr(st);
System.setOut(st);

Is This Answer Correct ?    6 Yes 1 No

Post New Answer

More Core Java Interview Questions

How would you dynamically allocate memory to an array?

0 Answers   Atos Origin,


What is the catch or declare rule for method declarations?

1 Answers  


What are loops in java?

2 Answers  


How many threads can I run java?

0 Answers  


Can a constructor be protected?

0 Answers  


How do you end a program?

0 Answers  


what is the swingutilities.invokelater(runnable) method for? : Java thread

0 Answers  


Is java hashset ordered?

0 Answers  


Program to Find the second largest element in an array.

0 Answers   Amazon,


How does a for loop work java?

0 Answers  


What are the parts of methodology?

0 Answers  


What is constructor

9 Answers   Manforce, Tech Mahindra,


Categories