PrintStream 打印流
打印流的好處:
- 打印流可以打印任意類型的數(shù)據(jù)灸撰。
- 打印流打印任意類型數(shù)據(jù)之前,會(huì)先把數(shù)據(jù)轉(zhuǎn)成字符串然后再打印出去睦袖。
我們使用字節(jié)流或者字符流寫出int類型數(shù)據(jù)的時(shí)候會(huì)比較麻煩习蓬,因?yàn)槲覀冃枰堰@些數(shù)據(jù)線轉(zhuǎn)換成字符串然后我們才能寫出去。
public class Demo1 {
public static void main(String[] args) throws IOException {
/*//找到目標(biāo)文件
File file = new File("F:\\a.txt");
//建立數(shù)據(jù)的輸出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//寫出數(shù)據(jù)
fileOutputStream.write("97".getBytes());
//關(guān)閉資源
fileOutputStream.close();
作用1: 可以為我們打印任意類型的數(shù)據(jù)降盹,而且打印數(shù)據(jù)之前會(huì)先轉(zhuǎn)換成字符串然后再打印与柑。
//找到目標(biāo)文件
File file = new File("F:\\a.txt");
//建立打印流對(duì)象
PrintStream printStream = new PrintStream(file);
//打印數(shù)據(jù)
printStream.println(97);
printStream.println(true);
printStream.println(3.14);
//關(guān)閉資源
printStream.close();
作用2: 收集日志信息
FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\20150519.log"), true);
PrintStream printStream = new PrintStream(fileOutputStream);
try{
String str=null;
System.out.println("字符個(gè)數(shù):"+ str.length());
int result = 4/0;
}catch(Exception e){
e.printStackTrace(printStream);
}
標(biāo)準(zhǔn)的輸出流默認(rèn)是指控制臺(tái),可以進(jìn)行修改蓄坏。
*/
PrintStream printStream = new PrintStream(new File("F:\\b.txt"));
//修改標(biāo)準(zhǔn)的輸出流
System.setOut(printStream);
System.out.println("hehe");
}
}