JUnit test for System.out.println()

using ByteArrayOutputStream and System.setXXX is simple: private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; private final PrintStream originalErr = System.err; @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @After public void restoreStreams() { System.setOut(originalOut); System.setErr(originalErr); } sample test cases: @Test public … Read more

Changing default encoding of Python?

Here is a simpler method (hack) that gives you back the setdefaultencoding() function that was deleted from sys: import sys # sys.setdefaultencoding() does not exist, here! reload(sys) # Reload does the trick! sys.setdefaultencoding(‘UTF8’) (Note for Python 3.4+: reload() is in the importlib library.) This is not a safe thing to do, though: this is obviously … Read more

How to clear the console?

Since there are several answers here showing non-working code for Windows, here is a clarification: Runtime.getRuntime().exec(“cls”); This command does not work, for two reasons: There is no executable named cls.exe or cls.com in a standard Windows installation that could be invoked via Runtime.exec, as the well-known command cls is builtin to Windows’ command line interpreter. … Read more

How can I get the application’s path in a .NET console application?

System.Reflection.Assembly.GetExecutingAssembly().Location1 Combine that with System.IO.Path.GetDirectoryName if all you want is the directory. 1As per Mr.Mindor’s comment: System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. … Read more