/*
* 打印流PrintStream
* 作用:
* 1.可以打印任何類的數(shù)據(jù)泻红,打印數(shù)據(jù)之前都會(huì)先把數(shù)據(jù)轉(zhuǎn)換成字符串再進(jìn)行打印;
* 2.收集異常的日志信息哪痰;
*
* 說明:默認(rèn)標(biāo)準(zhǔn)輸出流就是向控制臺輸出的粘拾,的System中的out就是一個(gè)PrintStream類對象,可以通過setOut來重新定義輸入的位置
*
*/
package com.michael.lin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
class User{
String name;
String sex;
public User(String name, String sex){
this.name = name;
this.sex = sex;
}
public String toString(){
return "姓名"+this.name + this.sex +"性別";
}
}
public class Demo6 {
public static void main(String[] args) throws FileNotFoundException{
//1.定位文件
File file = new File("c:\\a.txt");
//2.聲明打印流
PrintStream printStream = new PrintStream(file);
//1.作用1:打印任何類型的數(shù)據(jù)
/*User user = new User("金哥","男");
printStream.print(user);
printStream.print(97);
printStream.print(true);*/
//重新設(shè)置標(biāo)準(zhǔn)的輸出流對象.類似于輸出重定向
System.setOut(printStream);
System.out.println("你好啊");
//2.收集錯(cuò)誤信息并打印到日志
File file1 = new File("c:\\log.txt");
//PrintStream printStream1 = new PrintStream(file1); 每次寫入都會(huì)清空
//以寫入的方式打印到日志文件
PrintStream printStream1 = new PrintStream(new FileOutputStream(file1,true));
while(true){
try{
int a = 4/0;
}catch(Exception e){
e.printStackTrace(printStream1);
}
}
}
}