System.out.println not functioning

前端 未结 6 1114
囚心锁ツ
囚心锁ツ 2021-01-19 11:54

What are some scenarios in which java\'s System.out.println would fail to produce any output. I have a call to it inside of a method and sometimes when the method is called

6条回答
  •  走了就别回头了
    2021-01-19 12:16

    It's possible that the file handle has been changed. I.e., stdout's file descriptor is no longer 1. I've seen this done in logging utilities where people don't want to go and catch any text that might be printed to a file descriptor, so instead they just redirect the stream to a file handle of an open file.

    Here's an example in python:

    import sys
    
    h = open(r"C:\foo.txt","a+")
    
    sys.stdout = h
    sys.stdout.write("hey fellas")
    
    h.close()
    

    Run this at the cmdline, and you'll not get "hey fellas" printed out as you expect. Instead, it will be redirected to the file C:\foo.txt

提交回复
热议问题