printstream

Why don't we close `System.out` Stream after using it?

巧了我就是萌 提交于 2019-11-26 23:28:02
问题 I just want to know, we usually close streams at the end, but why don't we close System.out PrintStream with System.out.close() ? 回答1: If you close it you will no longer be able to write to the console, so let's leave this task to the VM when the process terminates. You should only close streams that you own or have manually created. System.out is out of your control, so leave it to the creator to take care of. 回答2: because we didn't open it the VM did and it's his job to close it unless

Printing Runtime exec() OutputStream to console

拜拜、爱过 提交于 2019-11-26 12:26:53
I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done? Here is some incomplete code: import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; public class RuntimeTests { public static void main(String[] args) { File path = new File("C:\\Dir\\Dir2"); String command = "cmd /c dir"; Reader rdr = null; PrintStream prtStrm = System.out; try { Runtime terminal = Runtime.getRuntime(); OutputStream rtm = terminal.exec(command, null, path)

Writing to console and text file

社会主义新天地 提交于 2019-11-26 09:50:15
问题 I found the code below from the internet, works but it doesn\'t write the printed console to omt.txt, it only writes the System.out.println statements after the second catch block.If you run the code once you will understand what I mean.All I want is to write what is on console to the \"omt.txt\" file that is all... After some answers, I see that my question wasn\'t clear, sorry for that. I want to save console output to omt.txt text file. If on the console \"Hello 123\" is printed , it

Java: Difference between PrintStream and PrintWriter

亡梦爱人 提交于 2019-11-26 08:45:58
问题 What is the difference between PrintStream and PrintWriter ? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise, there would have been only one class. I have searched the archives, but couldn\'t find this question. 回答1: This might sound flippant, but PrintStream prints to an OutputStream , and PrintWriter prints to a Writer . Ok, I doubt I'll get any points

Is multi-thread output from System.out.println interleaved

﹥>﹥吖頭↗ 提交于 2019-11-26 05:49:00
问题 If multiple threads call System.out.println(String) without synchronization, can the output get interleaved? Or is the write of each line atomic? The API makes no mention of synchronization, so this seems possible, or is interleaved output prevented by buffering and/or the VM memory model, etc.? EDIT: For example, if each thread contains: System.out.println(\"ABC\"); is the output guaranteed to be: ABC ABC or could it be: AABC BC 回答1: Since the API documentation makes no mention of thread

Printing Runtime exec() OutputStream to console

一世执手 提交于 2019-11-26 02:57:38
问题 I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done? Here is some incomplete code: import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; public class RuntimeTests { public static void main(String[] args) { File path = new File(\"C:\\\\Dir\\\\Dir2\"); String command = \"cmd /c dir\"; Reader rdr = null; PrintStream prtStrm = System.out