1、打印流(printStream)的概念
打印流可以打印任意的数据类型
2、printStream的步骤
1.找到目标文件
2.创建一个打印流
3.打印信息
4.关闭资源
3、实例
package com.dhb.file; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream; /**
* @author DSHORE / 2018-7-19
*
*/
/* 打印流(printStream):打印流可以打印任意的数据类型
* */
class Animal{
String name;
String color;
public Animal(String name, String color) {
super();
this.name = name;
this.color = color;
}
@Override
public String toString() {
return "Animal [name=" + name + ", color=" + color + "]";
}
} public class Demo25 {
public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("F:\\e.txt");
//创建一个打印流
PrintStream ps = new PrintStream(file);
//打印信息
ps.println();
ps.println(3.14);
Animal a = new Animal("二狗子", "咖啡色");
ps.println(a);
ps.println('a'); System.setOut(ps);//重新设置标准打印流对象
System.out.println("hello,哈罗!"); //收集异常日志信息
File logFile=new File("F:\\2018年7月19日.log");
PrintStream logPrintStream=new PrintStream(new FileOutputStream(logFile),true);
for (int i = ; i <; i++) {
try {
int c=/;
} catch (Exception e) {
e.printStackTrace(logPrintStream);//打印到logPrintStream对象中,即:2016年9月3日.log文件上
}
}
//关闭资源
ps.close();
}
}
运行结果图
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/9337069.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |