java小知识-将System.out.println到控制台的内容打印并保存在新的文件中(例如txt文件从)

import java.io.FileOutputStream;
import java.io.PrintWriter; 
import java.io.PrintStream; 
import java.io.FileNotFoundException;
String strJSON = "这是一段字符串,这段字符串将会打印到指定文件中。";
//日志输出到指定文件中
PrintStream out;
try {
out = new PrintStream("C://Users//whr-pc//Desktop//classpath//img//Systemout.txt");

System.setOut(out);
System.out.println(strJSON);
} catch (FileNotFoundException e) {
e.printStackTrace();
}  

发布了149 篇原创文章 · 获赞 132 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Sailor_luo/article/details/79207533