一、先看下必備知識:
1,流:系統(tǒng)內(nèi)部和外部進(jìn)行數(shù)據(jù)傳輸?shù)墓艿馈?/p>
2,內(nèi)部:內(nèi)存;外部:輸入設(shè)備,文件,網(wǎng)絡(luò)。
3,輸入(讀)通過輸入流對象,從外把數(shù)據(jù)傳送到內(nèi)部。
4,輸出(寫),從內(nèi)到外,通過輸出流對象,把內(nèi)存數(shù)據(jù)傳送到外部(網(wǎng)絡(luò),文件,顯示器控制臺)。
5,文本文件:
以ASCII碼方式存放的文件,一般一個英文字符一個字節(jié)(以字節(jié)為單位),.txt某種意義上顯示器和鍵盤也叫文本文件。
如果想看見文件的內(nèi)容,這個文件必須以文本方式存放,所以顯示器也是文本文件,與內(nèi)存存放方式(二進(jìn)制)unicode碼不一樣,將文件輸入內(nèi)存,使用需要轉(zhuǎn)換。
缺點:文本文件如果處理需要轉(zhuǎn)換,效率低
6,二進(jìn)制文件:
以內(nèi)存方式存放,內(nèi)部怎么用外部怎么存,不可見,不能通過外部設(shè)備輸入
優(yōu)點:處理不需要轉(zhuǎn)換,效率高,使用容易。
//***
流:嚴(yán)格意義上只有一種,就是字節(jié)流,jdk1.2版本增加了字符流,其實也是字節(jié)流的變體。
字節(jié)流傳輸太慢,又有過濾流(BufferedStream,DataStream,ObjectStream對象流)來套接,也叫套接流,假設(shè)字節(jié)流是個小管子,那么過濾流就是大管子,把大管子套接在小管子上,將字節(jié)流作為這些流的參數(shù)。另外,標(biāo)準(zhǔn)字節(jié)輸入輸出流(System.out/in/array);還有管道流(PipedStream),經(jīng)常用于線程之間;順序輸入流(SequenceStream),將小管子接在一起成為長管子,等等,這些也全是字節(jié)流,內(nèi)存流很少使用。
二、常用流
1、字節(jié)流
字節(jié)流是一個抽象類,不是接口,因為輸入字節(jié)流的read()方法沒實現(xiàn),輸出字節(jié)流的write()方法沒實現(xiàn)。不管是什么字節(jié)流,字節(jié)流的基類是inputStream OutputStream。
(1)inputStream 的一些常用方法:
對read做重載(三個),
int read();//從輸入流中讀一個字節(jié),形成一個0~255之間的整數(shù)返回(是一個抽象方法)。
int read(byte b[]);//從流中讀取若干個字節(jié)的數(shù)據(jù),存放到字節(jié)數(shù)組中,用于struts文件的上傳
int read(byte b[],int off,int len);//從輸入的字節(jié)流讀取lenth個字節(jié),從offset開始,放到字節(jié)數(shù)組中
close();//關(guān)閉
(2)OutputStream的一些常用方法:
對write做重載(三個),
write(int b);//將一個整數(shù)最低字節(jié)數(shù)據(jù)輸出到流中,
write(byte b[])//把內(nèi)存的字節(jié)數(shù)組里的數(shù)據(jù)依次輸出到流所指向的文件
write(byte b[],int off ,int length)//將數(shù)組b中從off指定的位置開始,長度為len的數(shù)據(jù)輸出到流中;
flush();//刷空輸出流,并將緩沖區(qū)中的數(shù)據(jù)強制送;一定要刷空。
close();//關(guān)閉
2、文件字節(jié)流
(1)文件字節(jié)流創(chuàng)建出輸入流和輸出流對象,
(2)文件輸入和輸出只作文本文件,以順序操作,并且區(qū)分輸入流和輸出流。
例
import java.io.*;
class Filestream
{
public static void main(String args[])
{
try
{
File inFile=new File("file1.txt");
File outFile=new File("file2.txt");
//創(chuàng)建出輸入流和輸出流對象,
FileInputStream fis=new FileInputStream(inFile);
FileOutputStream fos=new ?FileOutputStream(outFile);
int c;
while((c=fis.read())!=-1) ?fos.write(c);
fis.close();//關(guān)閉
fos.close();
}catch(FileNotFoundException e) {
System.out.println("FileStreamsTest: "+e);
}catch(IOException e) {
System.err.println("FileStreamsTest: "+e);
}
}
}
3、隨機(jī)流
(1)類RandomAccessFile允許對文件內(nèi)容同時完成讀和寫操作,它直接繼承object,并且同時實現(xiàn)了接口DataInput和DataOutput,提供了支持隨機(jī)文件操作的方法:
(2)不區(qū)分輸入流輸出流,對文件訪問一定要指定訪問方式,(rw)可以隨機(jī)訪問文件某一位置的字符,可以以二進(jìn)制的方式對數(shù)據(jù)進(jìn)行存儲,可以隨機(jī)調(diào)整文件位置指針,隨機(jī)輸出。
(3)經(jīng)典面試?yán)?/p>
例
import java.io.*;
public class random_file
{
public static void main(String args[])
{
int[] data_arr = {12,31,56,23,27,1,43,65,4,99};
try
{
RandomAccessFile randf = new RandomAccessFile("temp.dat","rw");
for (int i = 0; i < data_arr.length; i++)
randf.writeInt(data_arr[i]);
for(int i=data_arr.length-1; i>=0; i--)
{
randf.seek(i*4);
System.out.println(randf.readInt());//倒敘輸出
}
randf.seek(8);//跳8個字節(jié)輸出結(jié)果
System.out.println(randf.readInt());//56
System.out.println(randf.readInt());//23
randf.skipBytes(8);
System.out.println(randf.readInt());//43
randf.close();
}catch (IOException e){
System.out.println("File access error: "+e);
}
}
}
4、過濾流
(1)BufferStream
BufferInputStream和BufferOutputStream必須套接在字節(jié)流上,順序讀取;
Buffer流是過濾流,不能單獨使用,并且區(qū)分輸入流和輸出流;
只能處理文本文件,不能一次讀一行,一次能讀取若干字節(jié)的字符,沒有做本質(zhì)的改變;
例
import java.io.*;
public class BufferedStreamDemo{
public static void main(String[] args){
try{
FileInputStream in = new FileInputStream(".//data1.txt");
BufferedInputStream fin = new BufferedInputStream(in);
FileOutputStream out = new FileOutputStream(".//data2.txt");
BufferedOutputStream fout = new BufferedOutputStream(out);
byte[] buf = new byte[512];
int c = 0;
while((c = fin.read(buf, 0, 512)) != -1){
System.out.println("c = " + c);
fout.write(buf, 0, c);
}
fin.close();
fout.close();
}catch(FileNotFoundException e){
System.out.println("File Not Found!");
}catch(IOException e){
System.out.println("I/O Error!");
}
}
}
(2)DataStream
Data流也是套接流,一定要套接在字節(jié)流上,區(qū)分輸入輸出
DataInputStream
DataOutputStream
既可以套接文件字節(jié)流,也可以套接在網(wǎng)絡(luò)上(很多)
(1)套接在文件流上
(2)可以一次讀取一行的字符
(3)可以對漢字做編碼
(4)不僅處理文本文件,也可以處理二進(jìn)制文件
例
import java.io.*;
public class datainput_output
{
public static void main(String args[])throws IOException
{
FileOutputStream fos = new FileOutputStream("a.txt");
DataOutputStream dos = new DataOutputStream (fos);
try
{
dos.writeBoolean(true);
dos.writeByte((byte)123);
dos.writeChar('J');
dos.writeDouble(3.141592654);
dos.writeFloat(2.7182f);
dos.writeInt(1234567890);
dos.writeLong(998877665544332211L);
dos.writeShort((short)11223);
}
finally
{
dos.close();
}
FileInputStream ?fis = new FileInputStream("a.txt");
DataInputStream dis = new DataInputStream(fis);
try
{
System.out.println("\t "+dis.readBoolean());
System.out.println("\t "+dis.readByte());
System.out.println("\t "+dis.readChar());
System.out.println("\t "+dis.readDouble());
System.out.println("\t "+dis.readFloat());
System.out.println("\t "+dis.readInt());
System.out.println("\t "+dis.readLong());
System.out.println("\t "+dis.readShort());
}
finally
{
dis.close();
}
}
}
(3)ObjectStream
也是過濾流,只處理二進(jìn)制文件,不能處理文本文件
序列化:(串行化):把自己的狀態(tài)(屬性值)放在對象輸出流中傳送就叫。。
1,這個類一定要加入串行化協(xié)議String類和封裝類就加入了串行化協(xié)議。
2,創(chuàng)建出類的對象,new Student(),為類的實例全局變量賦值。
3,創(chuàng)建對象輸出流,先創(chuàng)建字節(jié)流,再套接在對象流上。
4,對象輸出流對象的writeObject()方法,寫進(jìn)流中。
例
public class Student implements Serializable//串行化協(xié)議
{
int id;
String name;
int age;
String department;
public Student(int id, String name, int age, String department)
{
this.id = id;
this.name = name;
this.age = age;
this.department = departmernt;
}
}
反序列化:
通過文件對象輸入流讀取對象的狀態(tài),再生出對象。
1,創(chuàng)建對象輸入流對象,先創(chuàng)建文件輸入流對象。
2,從對象所指文件讀取對象的狀態(tài),readObject()再生成對象object造型為student。
3,串行化加反串行化叫做對象的持續(xù)性。
對象的串行化只保存對象的屬性(狀態(tài))。
瞬時的狀態(tài)也不可保存,用transient標(biāo)注,這時對象屬性不能傳送。
例
public class Objectser
{
public static void main(String args[])
{
Student stu = new Student(981036,“Li Ming”, 16,“CSD”);
try
{
FileOutputStream fo = new FileOutputStream(“data.ser”);
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(stu);
so.close();
}
catch(Exception e)
{
System.out.println(e) ;
}
}
}
5、管道流PipedStream
分管道輸入流和輸出流 ,在線程中傳數(shù)據(jù),生產(chǎn)者消費者模式與這個相結(jié)合。
例
import java.io.*;
/**
*管道流和生產(chǎn)者消費者相結(jié)合
*@TestPipePC測試管道流和線程結(jié)合類
*@Wang Cy 2016/0803
*/
public class TestPipePC
{
public static void main(String[] args)
{
//創(chuàng)建管道輸出流
PipedInputStream pis = ?new PipedInputStream();
//創(chuàng)建管道輸出流
try{
PipedOutputStream pos = new PipedOutputStream(pis);
//生產(chǎn)者線程
Producer p = new Producer(pos);
//創(chuàng)建消費者線程
Consumer c = new Consumer(pis);
//激活線程
p.start();
c.start();
}catch(IOException e){
System.out.println(e);
}
//管道流是不是無法實現(xiàn)三個或以上的線程通信
}
}
//生產(chǎn)者線程
class Producer extends Thread
{
private PipedOutputStream pos;
public Producer(PipedOutputStream pos)
{
this.pos = pos;
}
public void run()
{
int i = 0;
try {
while(i<10)
{
this.sleep(1000);
pos.write(i);
System.out.println("生產(chǎn)者:"+i);
i++;
}
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException e2){
e2.printStackTrace();
}
}
}
//消費者線程
class Consumer extends Thread
{
private PipedInputStream pis;
public Consumer(PipedInputStream pis)
{
this.pis = pis;
}
public void run()
{
try
{
while(true)
{
System.out.println("消費者:"+pis.read());
}
} catch (IOException e1) {
}
}
}
6、順序輸入流SequenceStream
只對文件字節(jié)輸入流做鏈接
import java.io.*;
public class SequenceStream
{
public static void main(String args[])
{
try
{
FileInputStream f1,f2;
String s;
f1 = new FileInputStream("file1.txt");
f2 = new FileInputStream("data1.txt");
SequenceInputStream fs = new SequenceInputStream(f1, f2);//對兩個輸入流做連接
DataInputStream ds = new DataInputStream(fs);
while( (s = ds.readLine()) != null )
System.out.println(s);
f1.close();
f2.close();
}catch(FileNotFoundException e) {
System.out.println("FileStreamsTest: "+e);
}catch(IOException e) {
System.err.println("FileStreamsTest: "+e);
}
}
}
7、PrintStream標(biāo)準(zhǔn)流
被system繼承,加載主方法時,通過system創(chuàng)建3個對象,in,out,err
8、字符流
字符流不能讀取二進(jìn)制文件,只能處理文本文件。
import java.io.*;
public class FileTOUnicode
{
public static void main(String args[])
{
try
{
FileInputStream fis = new FileInputStream("file1.txt");
InputStreamReader dis = new InputStreamReader(fis);
// InputStreamReader dis = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(dis);
String s;
while( (s = reader.readLine()) != null )
{
System.out.println("read: "+s);
}
dis.close();
}catch(IOException e)
{
System.out.println(e);
}
}
}
?"?????u?vB??