Tengo un programa Test.java:Redirigir Runtime.getRuntime(). Exec() salida con System.setOut();
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream(new FileOutputStream("test.txt")));
System.out.println("HelloWorld1");
Runtime.getRuntime().exec("echo HelloWorld2");
}
}
Esto se supone que imprimir HelloWorld1 y HelloWorld2 a la text.txt archivo. Sin embargo, cuando veo el archivo, solo veo HelloWorld1.
¿Dónde estaba HelloWorld2? ¿Se desvaneció en el aire?
Digamos que quiero redirigir HelloWorld2 a test.txt también. No puedo simplemente agregar un ">> test.txt" en el comando porque obtendré un error de archivo ya abierto. Entonces, ¿cómo hago esto?
¿Es un requisito utilizar Runtime? – Navi
@Navi: ¿Hay una alternativa? Contar. ¡Quiero saber! ¿O quiere usar ProcessBuilder? –