Posted on 2007-03-05 22:41
Fisher 閱讀(229)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
Java基礎(chǔ)
1、流:
它是通過(guò)緩沖機(jī)制將數(shù)據(jù)從生產(chǎn)者(如鍵盤(pán)、磁盤(pán)文件、內(nèi)存或其他設(shè)備)傳送到接受該數(shù)據(jù)的消費(fèi)者(如屏幕、文件或者內(nèi)存等)的這一過(guò)程的抽象。
2、有關(guān)的Java包:
Java.io包中包括許多類(lèi)提供許多有關(guān)文件的各個(gè)方面操作。
3、有關(guān)文件名及目錄名的類(lèi):File 類(lèi)獨(dú)立于系統(tǒng)平臺(tái),利用構(gòu)造函數(shù)
File( String path)、
File(String path, String FileName)、
File(File dir, String name) 等創(chuàng)建出File 對(duì)象;再利用canRead() 、canWrite()、 getParent()、 getPath()等成員函數(shù)實(shí)現(xiàn)對(duì)文件的各個(gè)屬性的操作。
import java.io.*;
public class FileTest
{ public static void main(String []args)
{
String FileName="C:\\temp\\myfile.dat"
File myFile=new File(FileName);
If( ! myFile. exists() )
{ System.err.println("Can't Find " + FileName);
return;
}
System.out.println("File " + FileName + "is " +myFile.length() + "bytes Long !");
If( myFile. isDirectory() )
{ System.err.println("File" + FileName +"Is a Directory !");
return;
}
}
}
4、有關(guān)文件內(nèi)容(數(shù)據(jù))操作的類(lèi):
4.1 輸入輸出抽象基類(lèi)InputStream/OutputStream ,實(shí)現(xiàn)文件內(nèi)容操作的基本功能函數(shù)read()、 write()、close()、skip()等;一般都是創(chuàng)建出其派生類(lèi)對(duì)象(完成指定的特殊功能)來(lái)實(shí)現(xiàn)文件讀寫(xiě)。在文件讀寫(xiě)的編程過(guò)程中主要應(yīng)該注意異常處理的技術(shù)。
4.2 FileInputStream/FileOutputStream:
用于本地文件讀寫(xiě)(二進(jìn)制格式讀寫(xiě)并且是順序讀寫(xiě),讀和寫(xiě)要分別創(chuàng)建出不同的文件流對(duì)象);
本地文件讀寫(xiě)編程的基本過(guò)程為:
① 生成文件流對(duì)象(對(duì)文件讀操作時(shí)應(yīng)該為FileInputStream類(lèi),而文件寫(xiě)應(yīng)該為FileOutputStream類(lèi));
② 調(diào)用FileInputStream或FileOutputStream類(lèi)中的功能函數(shù)如read()、write(int b)等)讀寫(xiě)文件內(nèi)容;
③ 關(guān)閉文件(close())。
4.3 PipedInputStream/PipedOutputStream:
用于管道輸入輸出(將一個(gè)程序或一個(gè)線(xiàn)程的輸出結(jié)果直接連接到另一個(gè)程序或一個(gè)線(xiàn)程的輸入端口,實(shí)現(xiàn)兩者數(shù)據(jù)直接傳送。操作時(shí)需要連結(jié));
4.3.1 管道的連接:
方法之一是通過(guò)構(gòu)造函數(shù)直接將某一個(gè)程序的輸出作為另一個(gè)程序的輸入,在定義對(duì)象時(shí)指明目標(biāo)管道對(duì)象
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream(pInput);
方法之二是利用雙方類(lèi)中的任一個(gè)成員函數(shù) connect()相連接
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream();
pinput.connect(pOutput);
4.3.2 管道的輸入與輸出:
輸出管道對(duì)象調(diào)用write()成員函數(shù)輸出數(shù)據(jù)(即向管道的輸入端發(fā)送數(shù)據(jù));而輸入管道對(duì)象調(diào)用read()成員函數(shù)可以讀起數(shù)據(jù)(即從輸出管道中獲得數(shù)據(jù))。這主要是借助系統(tǒng)所提供的緩沖機(jī)制來(lái)實(shí)現(xiàn)的。
4.4、隨機(jī)文件讀寫(xiě):
RandomAccessFile類(lèi)(它直接繼承于Object類(lèi)而非InputStream/OutputStream類(lèi)),從而可以實(shí)現(xiàn)讀寫(xiě)文件中任何位置中的數(shù)據(jù)(只需要改變文件的讀寫(xiě)位置的指針)。
隨機(jī)文件讀寫(xiě)編程的基本過(guò)程為:
① 生成流對(duì)象并且指明讀寫(xiě)類(lèi)型;
② 移動(dòng)讀寫(xiě)位置;
③ 讀寫(xiě)文件內(nèi)容;
④ 關(guān)閉文件。
StringBuffer buf=new StringBuffer();
char ch;
while( (ch=(char)System.in.read()) !='\n')
{
buf.append( ch);
} //讀寫(xiě)方式可以為"r" or "rw"
RandomAccessFile myFileStream=new RandomAccessFile("myFile.dat"," rw");
myFileStream . seek(myFileStream.length()) ;
myFileStream.writeBytes(buf.toString()); //將用戶(hù)從鍵盤(pán)輸入的內(nèi)容添加到文件的尾部
myFileStream.close();
4.5 DataInput/DataOutput接口:實(shí)現(xiàn)與機(jī)器無(wú)關(guān)的各種數(shù)據(jù)格式讀寫(xiě)(如readChar() 、readInt()、readLong()、readFloat(),而readLine()將返回一個(gè)String)。其中RandomAccessFile類(lèi)實(shí)現(xiàn)了該接口,具有比FileInputStream或FileOutputStream類(lèi)更靈活的數(shù)據(jù)讀寫(xiě)方式。
4.6 標(biāo)準(zhǔn)輸入輸出流:System.in(如:char c=System.in.read())和System.out(如:System.out.println()、System.out.println())。
try
{ char ch=System.in.read(); //返回二進(jìn)制數(shù)據(jù)(低8位為鍵盤(pán)的ASCII碼)
}
catch(IOException e)
{
}
4.7、文件操作的一般方法:
(1)生成一個(gè)輸入輸出文件類(lèi)的對(duì)象(根據(jù)所要操作的類(lèi)型);
(2)調(diào)用此類(lèi)的成員函數(shù)實(shí)現(xiàn)文件數(shù)據(jù)內(nèi)容的讀寫(xiě);
(3)關(guān)閉此文件。
?