java重定向标准输出流到当前目录的log.txt文件,并输出如下语句:
Thisisalogfile.****************************************************Time:2014.1.16xx:x...
This is a log file.
****************************************************
Time:2014.1.16 xx:xx:xx
Now system is running well.
My name is XXX.
其中time行的日期和时间是实时获得,不能写死 展开
****************************************************
Time:2014.1.16 xx:xx:xx
Now system is running well.
My name is XXX.
其中time行的日期和时间是实时获得,不能写死 展开
1个回答
展开全部
package com.kidd.atmtest;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("log.txt文件位置:"
+ Thread.currentThread().getContextClassLoader()
.getResource("log.txt").getFile());
System.setOut(new PrintStream(Thread.currentThread()
.getContextClassLoader().getResource("log.txt").getFile()));
System.out.println("This is a log file.");
System.out
.println("****************************************************");
System.out.println("Time:"
+ new SimpleDateFormat("yyyy.MM.dd HH:mm:ss")
.format(new Date()));
System.out.println("Now system is running well.");
System.out.println("My name is Test.");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询