I got problem in java. I need to use FileWriter to write output. But now i want to use Filewriter to write standard output( to console). Have any ideas?? Thanks for your helping
Asked
Active
Viewed 1,113 times
-4
-
Nomen est omen. **File**Writer. – Robby Cornelissen Mar 26 '18 at 01:37
-
Who has given you this patently ridiculous requirement and why? – Hovercraft Full Of Eels Mar 26 '18 at 01:37
-
@RobbyCornelissen stdout (also stderr and stdin) can be identified and written to like any normal file via a filedescriptor. And that's actually just what java does: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.FileDescriptor) – Mar 26 '18 at 02:22
1 Answers
3
This can be done by initializing the FileWriter
with a FileDescriptor
via the constructor FileWriter#FileWriter(FileDescriptor)
A quick example:
import java.io.*;
public class Test
{
public static void main(String[] args)
throws IOException
{
FileWriter fw = new FileWriter(FileDescriptor.out);
fw.write("Hello world\n");
fw.flush();
fw.close();
}
}
Outputs
Hello world