在整個IO包中计螺,打印流是輸出信息最方便的類雕拼。
PrintStream(字節(jié)打印流)和PrintWriter(字符打印流)
提供了一系列重載的print和println方法琅束,用于多種數(shù)據(jù)類型的輸出
PrintStream和PrintWriter的輸出不會拋出異常
PrintStream和PrintWriter有自動flush功能
System.out返回的是PrintStream的實例
package com.atguigu.java;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.xml.crypto.Data;
import org.junit.Test;
public class TestOtherStream {
@Test
public void testData1(){
DataInputStream dis = null;
try{
dis = new DataInputStream(new FileInputStream(new File("Data.txt")));
// String str;
// while((str = dis.readLine()) != null){
// System.out.println(str);
// }
// byte[] b = new byte[20];
// int len;
// while((len = dis.read(b)) != -1){
// System.out.println(new String(b,0,len));
// }
String str = dis.readUTF();
System.out.println(str);
boolean b = dis.readBoolean();
System.out.println(b);
long l = dis.readLong();
System.out.println(l);
}catch(IOException e1){
e1.printStackTrace();
}finally{
if(dis != null){
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//數(shù)據(jù)流:用來處理基本數(shù)據(jù)類型纯命、String争涌、字節(jié)數(shù)組的數(shù)據(jù):DataInputStream DataOutputStream
@Test
public void testData(){
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream("data.txt");
dos = new DataOutputStream(fos);
dos.writeUTF("我愛你,而你卻不知道");
dos.writeBoolean(true);
dos.writeLong(1233333333);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(dos != null){
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//打印流:字節(jié)流:PrintStream 字符流:PrintWriter
@Test
public void printStreamWriter(){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("print.txt"));
} catch (Exception e) {
e.printStackTrace();
}
//創(chuàng)建打印輸出流音五,設(shè)置為自動刷新模式(寫入換行符或字節(jié)'\n'時都會刷新輸出緩沖區(qū))
PrintStream ps = new PrintStream(fos, true);
if(ps != null){
System.setOut(ps);//把標準輸出(控制臺輸出)改成文件輸出
}
for(int i = 0; i <= 255; i++){//輸出ASCII字符
System.out.print((char)i);
if (i % 50 == 0) {
System.out.println();
}
}
}
}