打印流:
這個(gè)流的主要作用就是打印文字期吓,它的目標(biāo)主要是文件早歇,屏幕,網(wǎng)絡(luò)讨勤。打印流屬于輸出流箭跳。它分為PrintStream,PrintWriter
PrintStream中的Print方法就是鍵盤錄入潭千,PrintStream中的Writer方法就是打印的是二進(jìn)制的后四位衅码。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class IoDemo{
public static void main(String[] args) throws IOException {
PrintStream ps=new PrintStream("e:/lishuai.txt");
ps.print(78);
ps.print("lishuai");
ps.println();
ps.write(85);
}
}
PrintWriter字符輸出流的打印流,當(dāng)在創(chuàng)建對(duì)象的時(shí)候,根據(jù)api中介紹的構(gòu)造方法能夠開啟了自動(dòng)刷新的功能脊岳,當(dāng)我們使用PrintWriter中的printf、println、format方法寫出數(shù)據(jù)的時(shí)候割捅,就會(huì)自動(dòng)刷新奶躯。
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class IoDemo{
public static void main(String[] args) throws IOException {
PrintWriter pw=new PrintWriter(new FileWriter("e:/lishuai.txt",true));
pw.println("lishualiu");
}
}
###管道流:
這個(gè)流的特點(diǎn)是可以把輸入和輸出流直接對(duì)接起來,我們不用在兩個(gè)之間定義任何的變量或者數(shù)組亿驾。PipedInputStream和PipedOutputStream 字節(jié)輸入和輸出的管道流PipedReader和PipedWriter字符輸入和輸出的管道流管道輸入流它是負(fù)責(zé)從管道讀取數(shù)據(jù)嘹黔,管道輸出流它負(fù)責(zé)把數(shù)據(jù)寫到管道中。
```java
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class IoDemo {
public static void main(String[] args) throws IOException {
PipedInputStream pi = new PipedInputStream();
PipedOutputStream po = new PipedOutputStream();
pi.connect(po);
Input in=new Input(pi);
Output ou=new Output(po);
Thread t1=new Thread(in);
Thread t2=new Thread(ou);
t1.start();
t2.start();
}
}
class Input implements Runnable {
PipedInputStream pi;
Input(PipedInputStream pi) {
this.pi = pi;
}
@Override
public void run() {
// TODO Auto-generated method stub
int len = 0;
byte[] b = new byte[1024];
try {
while ((len = pi.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Output implements Runnable {
PipedOutputStream po;
Output(PipedOutputStream po) {
this.po = po;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
po.write("李帥的輸出".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
po.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}