打印流
- 打印流可以打印任意类型的数据,而且打印数据之前会把数据转换成字符串再打印
- 可以收集异常的日志信息
package test;
import java.io.*;
class Animal{
String name;
String color;
public Animal(String name,String color){
this.name=name;
this.color=color;
}
@Override
public String toString() {
return "name:"+this.name+" color:"+this.color;
}
}
public class DemoPrint {
public static void main(String[] args) throws IOException{
File file=new File("F:\\a.txt");
PrintStream printStream=new PrintStream(file);
printStream.println(97);
printStream.println(5.02);
printStream.println('a');
printStream.println(true);
printStream.println("hello World");
Animal animal=new Animal("mouse","black");
printStream.println(animal);
File logFile=new File("F:\\test\\2016年12月12日.log");
PrintStream logPrintStream=new PrintStream(new FileOutputStream(logFile,true));
try{
int c=4/0;
System.out.println("c="+c);
}catch(Exception e){
e.printStackTrace(logPrintStream);
}
printStream.close();
}
}