Java program output to file

System.out to a file in java

I’m running an application from inside another one for testing purposes. I want to redirect the output for the tested app to a file, so I can have a log after each test. Is there a way to redirect the output of an app to a file from the command line in java?

it’s most likely going to be a solution external with respect to Java VM, like file descriptors redirection in bash/sh (e.g. «app.exe > file.log 2>&1»), unless you use some configurable logging library

7 Answers 7

You can use the output stream redirector that is supported by the Windows command line, *nix shells , e.g.

java -jar myjar.jar > output.txt 

Alternatively, as you are running the app from inside the vm, you could redirect System.out from within java itself. You can use the method

Which replaces the standard output stream, so all subsequent calls to System.out go to the stream you specify. You could do this before running your wrapped application, e.g. calling System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(«output.txt»))));

If you are using a wrapper that you can’t modify, then create your own wrapper. So you have FEST wrapper -> stream redirector wrapper -> tested app.

For example, you can implement a simple wrapper like this:

public class OutputRedirector < /* args[0] - class to launch, args[1]/args[2] file to direct System.out/System.err to */ public static void main(String[] args) throws Exception < // error checking omitted for brevity System.setOut(outputFile(args(1)); System.setErr(outputFile(args(2)); Class app = Class.forName(args[0]); Method main = app.getDeclaredMethod("main", new Class[] < (new String[1]).getClass()>); String[] appArgs = new String[args.length-3]; System.arraycopy(args, 3, appArgs, 0, appArgs.length); main.invoke(null, appArgs); > protected PrintStream outputFile(String name) < return new PrintStream(new BufferedOutputStream(new FileOutputStream(name)), true); >> 

You invoke it with 3 additional params — the Main class to run, and the output/error directs.

Читайте также:  Вывод питон новая строка

Источник

How to write all the System output to a file in JAVA?

I have created a JAVA application. Sometimes, users do invalid operations on it or there are some exceptions that the application encounters when it outputs the errors. However, these outputs are not visible unless I run the application from command line using java -jar myapp.jar I wish to record all of these to a file in the form of a log but I am unable to find a function or object which is responsible for outputting these errors etc. To simplify my explanation, assume that my application outputs number from 1 to 10 using a for loop and a Sytem.out command. How can I record everything that is output to the System? Thanks

This worked: FileOutputStream f = new FileOutputStream(«log-out.txt»); PrintStream ps = new PrintStream( f ); System.setOut(ps);

7 Answers 7

Why not use a log framework: http://logback.qos.ch/

With this you can easily switch between file or console.

Agreed with above, you should use a logging framework. I prefer to use abstractions such as SLF4J.

In this example you can use log4j underneath. Here is an example log4j.properties file you can put in the classpath of your java application.

#Rootlogger logs to console and logfile log4j.rootLogger=INFO,stdout,logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=/tmp/apname.log log4j.appender.logfile.MaxFileSize=1024KB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=3 # Pattern to output: date [thread] priority [category] - message log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d [%t] %p [%c] - %m%n #=============== CREATE A SEPARATE HANDLER FOR LOGGING SPECIFIC PACKAGES log4j.appender.MYPACKAGEHANDLER=org.apache.log4j.RollingFileAppender log4j.appender.MYPACKAGEHANDLER.File=/tmp/mypackage.log # 1/2 GB log4j.appender.MYPACKAGEHANDLER.MaxFileSize=512MB # Keep three backup files. log4j.appender.MYPACKAGEHANDLER.MaxBackupIndex=3 # Pattern to output: message only log4j.appender.MYPACKAGEHANDLER.layout=org.apache.log4j.PatternLayout log4j.appender.MYPACKAGEHANDLER.layout.ConversionPattern=%m%n log4j.additivity.com.techtrip.mypackage=false log4j.logger.com.techtrip.mypackage=DEBUG, MYPACKAGEHANDLER 

Using this config will create two log files with a rolling appender, printing all debug log output of any class that logs in the com.techtrip.mypackage to /tmp/mypackage.log.

Suppose a simple example class with a Logger and a Formatter:

package com.techtrip.mypackage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Foo < private Logger logger = LoggerFactory.getLogger(Foo.class); private String someString; private Foo() < super(); >public void setSomeString(String someString) < if (logger.isDebugEnabled())< logger.debug(String.format("Setting someString %s", someString)); >this.someString = someString; > > 

This will log the setter output to the log file. Turn it off by simply changing the property file. Very simple.

Источник

Most logging facilities support this kind of thing. For example, adding a ConsoleAppender and a FileAppender with Log4J will do this. Somebody mentioned SLF4J — which is true although its just a facade. As far as I know, you still need to configure which logging facility it will use behind the scenes.

5 Answers 5

You can include the following lines at the start of your program:

final PrintStream origout = System.out; final PrintStream fileout = new PrintStream(file); System.setOut(new PrintStream(new OutputStream() < @Override public void write(int b) throws IOException < origout.write(b); fileout.write(b); >>)); 

I am putting this at the beginning of my main method but I’m getting all sorts of errors, including undefined constructors etc. I imported all the appropriate packages. Please help

I got it to work by replacing new OutputStream with fileout. However, this is causing me to not be able to see any output in the console, rather I can only see output in the .txt file. How can I output to both?

You can use System.setOut() to redirect System.Out to a custom OutputStream that duplicates its output to both the console and a file.

You can write to a console. And write to a file. You can thread them seperately so they’re not dependent upon each other.

There isn’t an API for doing both at the same time that I’m aware of (not that that says too much, it should be trivial to write one).

Edit: Have I misunderstood? Do you mean from Java code or just piping the output of the java binary to console and a file? In which case you could do something like:

java Main 2>&1 | tee -a Load.log 

Источник

How to write console output to a txt file

I have tried to write the console output to a txt file using this code suggestion (http://www.daniweb.com/forums/thread23883.html#) however I was not successful. What’s wrong?

The code sample you gave writes the console input to a file. It’s not very clear what you are trying to achieve. Can you give more details?

I have many output on the console which resulted from system.out.println. I’m trying to write all these output to a .txt file.

11 Answers 11

You need to do something like this:

PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); 

The second statement is the key. It changes the value of the supposedly «final» System.out attribute to be the supplied PrintStream value.

There are analogous methods ( setIn and setErr ) for changing the standard input and error streams; refer to the java.lang.System javadocs for details.

A more general version of the above is this:

PrintStream out = new PrintStream( new FileOutputStream("output.txt", append), autoFlush); System.setOut(out); 

If append is true , the stream will append to an existing file instead of truncating it. If autoflush is true , the output buffer will be flushed whenever a byte array is written, one of the println methods is called, or a \n is written.

I’d just like to add that it is usually a better idea to use a logging subsystem like Log4j, Logback or the standard Java java.util.logging subsystem. These offer fine-grained logging control via runtime configuration files, support for rolling log files, feeds to system logging, and so on.

Alternatively, if you are not «logging» then consider the following:

    With typical shells, you can redirecting standard output (or standard error) to a file on the command line; e.g.

Changing System.out may cause nasty surprises for other code in your JVM that is not expecting this to happen. (A properly designed Java library will avoid depending on System.out and System.err , but you could be unlucky.)

Источник

Оцените статью