2 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
可以利用重定向?qū)崿F(xiàn)system函數(shù)輸出到文件。
例如:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Test {
publicstaticvoid main(String[] args) {
PrintStream printStream = null;
PrintStream sysout = System.out;
PrintStream syserr = System.err;
try {
File file = new File("c:\\systemout.log");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
printStream = new PrintStream(new FileOutputStream(new File(
"c:\\systemout.log"),true));
// set output to file instead of console
System.setOut(printStream);
System.setErr(printStream);
System.out.println("Before Redirect:System.out.println");
System.err.println("Before Redirect:System.err.println");
} catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (printStream !=null) {
printStream.close();
}
// Reset the output to console
System.setOut(sysout);
System.setErr(syserr);
System.out.println("After Redirect:System.out.println");
System.err.println("After Redirect:System.err.println");
}
}
}

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
最簡單的方法就是利用重定向?qū)崿F(xiàn)
system ( "ping 127.0.0.1 >xxxx" ); //xxxx為要寫入的文件名,包括路徑 |
- 2 回答
- 0 關(guān)注
- 88 瀏覽
添加回答
舉報(bào)