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

What is immutable in java?

0 Answers  


What are the parts of a method?

0 Answers  


What is used of static keyword in java?

0 Answers   Cyient,


what is the use of declaring constructor as private?

5 Answers   Cyient, IVY Technologies, Sai Softech, Virtusa,


What are different types of inner classes ?

0 Answers  






Does java trim remove newline?

0 Answers  


What is the indent key?

0 Answers  


What does this () mean in java?

0 Answers  


How do you sort in java?

0 Answers  


What is the difference(or similarity if there are some) between object and a variable?

4 Answers   ME,


Why to use nested classes in java?

0 Answers  


why java is better then .net?

5 Answers   HCL, iGate,


Categories