文件輸入輸出_第1頁(yè)
文件輸入輸出_第2頁(yè)
文件輸入輸出_第3頁(yè)
文件輸入輸出_第4頁(yè)
文件輸入輸出_第5頁(yè)
已閱讀5頁(yè),還剩45頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Java語(yǔ)言程序設(shè)計(jì)1JavaI/O操作概念JavaI/O類JavaI/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性2概念I(lǐng)/O來(lái)源控制臺(tái)(console,如DOS窗口)打印/讀入文件(file)讀/寫(xiě)網(wǎng)絡(luò)接口(TCP/UDP端口)讀/寫(xiě)針對(duì)數(shù)據(jù)的讀寫(xiě)以流(stream)的方式對(duì)數(shù)據(jù)進(jìn)行操作數(shù)據(jù)源程序讀出數(shù)據(jù)流目的程序?qū)懭霐?shù)據(jù)流流的重要特性順序讀/寫(xiě)sequentially3概念讀/寫(xiě)流的一般流程讀(Reading)openastream//打開(kāi)讀出流whilemoreinformation//判斷

readinformation//讀closethestream//關(guān)閉流寫(xiě)(Writing)openastream//打開(kāi)寫(xiě)入流whilemoreinformation//判斷

writeinformation//寫(xiě)closethestream//關(guān)閉流4概念兩種流的定義(讀取信息的基本數(shù)據(jù)單位)字節(jié)流(bytestream):一個(gè)字節(jié)(8-bit)一個(gè)字節(jié)讀/寫(xiě)字符流(characterstream):一個(gè)字符一個(gè)字符讀/寫(xiě)(具有特定字符編碼的數(shù)據(jù))java語(yǔ)言6A617661D3EFD1D4以字節(jié)流的方式讀:讀8次,8個(gè)字節(jié)以字符流的方式讀:讀6次,6個(gè)字符5JavaI/O操作概念JavaI/O類JavaI/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性6JavaI/O類字節(jié)流的讀/寫(xiě)操作(來(lái)自JDK1.0)java.io.InputStream(抽象類)publicabstract

intread()publicint

read(byteb[])publicint

read(byteb[],intoffset,intlength)到達(dá)流的終點(diǎn),無(wú)數(shù)據(jù)讀出則返回-1java.io.OutputStream(抽象類)publicabstractvoidwrite(intb)publicvoidwrite(byteb[])publicvoidwrite(byteb[],intoffset,intlength)所有的讀/寫(xiě)函數(shù)都拋出java.io.IOException7JavaI/O類字符流的讀/寫(xiě)操作(來(lái)自JDK1.1)java.io.Reader(抽象類)publicintread()publicint

read(char

cbuf[])publc

abstract

int

read(char

cbuf[],intoffset,intlength)到達(dá)流的終點(diǎn),無(wú)數(shù)據(jù)讀出則返回-1java.io.Writer(抽象類)publicvoidwrite(intc)publicvoidwrite(char

cbuf[])publicvoidwrite(char

cbuf[],intoffset,intlength)所有的讀/寫(xiě)函數(shù)都拋出java.io.IOException8JavaI/O類I/O流的層次關(guān)系classjava.io.InputStream的子類classjava.io.ByteArrayInputStreamclassjava.io.FileInputStreamclassjava.io.FilterInputStreamclassjava.io.BufferedInputStreamclassjava.io.DataInputStream

classjava.io.LineNumberInputStreamclassjava.io.PushbackInputStreamclassjava.io.ObjectInputStream

classjava.io.PipedInputStreamclassjava.io.SequenceInputStreamclassjava.io.StringBufferInputStream………9JavaI/O類I/O流的層次關(guān)系classjava.io.OutputStream的子類classjava.io.ByteArrayOutputStreamclassjava.io.FileOutputStreamclassjava.io.FilterOutputStreamclassjava.io.BufferedOutputStreamclassjava.io.DataOutputStream

classjava.io.PrintStreamclassjava.io.ObjectOutputStreamclassjava.io.PipedOutputStream………10JavaI/O類I/O流的層次關(guān)系classjava.io.Reader的子類classjava.io.BufferedReader

classjava.io.LineNumberReaderclassjava.io.CharArrayReaderclassjava.io.FilterReader

classjava.io.PushbackReaderclassjava.io.InputStreamReader

classjava.io.FileReaderclassjava.io.PipedReaderclassjava.io.StringReader………11JavaI/O類I/O流的層次關(guān)系classjava.io.Writer的子類classjava.io.BufferedWriterclassjava.io.CharArrayWriterclassjava.io.FilterWriterclassjava.io.OutputStreamWriter

classjava.io.FileWriterclassjava.io.PipedWriterclassjava.io.PrintWriterclassjava.io.StringWriter………12JavaI/O類I/O流的分類(12個(gè)功能類)I/O類型所用到的類描述文件FileReader/FileWriterFileInputStream/FileOutputStream文件流(讀/寫(xiě)文件)緩沖BufferedReader/BufferedWriterBufferedInputStreamBufferedOutputStream提高讀/寫(xiě)效率打印PrintWriter

PrintStream

(System.out.println())內(nèi)存CharArrayReader/CharArrayWriterByteArrayInputStreamByteArrayOutputStreamStringReader/StringWriterStringBufferInputStream讀/寫(xiě)內(nèi)存13JavaI/O類I/O流的分類(12個(gè)功能類)I/O類型所用到的類描述字節(jié)流和字符流的轉(zhuǎn)換InputStreamReaderOutputStreamWriter將InputStream中讀入的字節(jié)轉(zhuǎn)為字符/將字符轉(zhuǎn)為字節(jié)寫(xiě)入OutputStream管道pipePipedReader/PipedWriterPipedInputStream/PipedOutputStream文件流(讀/寫(xiě)文件)連接ConcatenationSequenceInputStream多個(gè)輸入流連接為一個(gè)輸入流對(duì)象串行化ObjectInputStreamObjectOutputStreamObjectSerialization14JavaI/O類I/O流的分類(12個(gè)功能類)I/O類型所用到的類描述基本數(shù)據(jù)轉(zhuǎn)化DataInputStreamDataOutputStreamJAVA基本數(shù)據(jù)類型的讀/寫(xiě)計(jì)數(shù)LineNumberReaderLineNumberInputStream讀操作時(shí)記錄行數(shù)PeekingAheadPushbackReaderPushbackInputStream可回退緩存(pushbackbuffer)過(guò)濾FilterReader/FilterWriterFilterInputStream/FilterOutputStream在讀/寫(xiě)操作時(shí)對(duì)數(shù)據(jù)進(jìn)行過(guò)濾15JavaI/O操作概念JavaI/O類JavaI/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性16JavaI/O操作主要內(nèi)容標(biāo)準(zhǔn)輸入/輸出控制臺(tái)屏幕打印和鍵盤(pán)讀入文件I/O操作文件讀寫(xiě)如何提高文件讀寫(xiě)效率流的包裝(Wrap)基本數(shù)據(jù)轉(zhuǎn)換流目錄管理隨機(jī)訪問(wèn)文件(RandomAccessFile)文件屬性17輸出:控制臺(tái)屏幕打印classTest{ publicstaticvoidmain(Stringargs[]){

System.out.println(“HelloWorld!”); }}標(biāo)準(zhǔn)輸入/輸出18輸入:鍵盤(pán)讀入importjava.io.IOException;classTest{ publicstaticvoidmain(Stringargs[])throws

IOException{ byte[]b=newbyte[10];

System.out.println("Receivednumber="+System.in.read(b)); }}標(biāo)準(zhǔn)輸入/輸出C:\>javaTestAReceivednumber=3C:\>19文件讀/寫(xiě)流程打開(kāi)文件流條件判斷讀出/寫(xiě)入關(guān)閉文件流兩種類型文件FileInputStream/FileOutputStream(字節(jié)流)FileReader/FileWriter(字符流)文件讀寫(xiě)操作20字節(jié)流構(gòu)造方法publicFileInputStream(Filefile)throwsFileNotFoundExceptionpublicFileInputStream(Stringname)throwsFileNotFoundExceptionpublicFile(Stringpathname)publicFileOutputStream(Filefile)throwsFileNotFoundExceptionpublicFileOutputStream(Filefile,booleanappend)throwsFileNotFoundException//是否向已存在的文件后添加publicFileOutputStream(Stringname)throwsFileNotFoundExceptionpublicFileOutputStream(Stringname,booleanappend)throwsFileNotFoundException文件讀寫(xiě)操作21文件讀寫(xiě)-實(shí)例1文件讀寫(xiě)操作importjava.io.*;publicclassCopyBytes{publicstaticvoidmain(String[]args)throwsIOException{FileinputFile=newFile(“original.txt");FileoutputFile=newFile(“result.txt");

FileInputStreamin=newFileInputStream(inputFile);

FileOutputStreamout=newFileOutputStream(outputFile);

intc;while((c=in.read())!=-1)

out.write(c);

in.close();

out.close();}}publicintread()throwsIOExceptionpublicintread(byte[]b)throwsIOExceptionpublicintread(byte[]b,intoff,int

len)throwsIOExceptionFileInputStreamin=newFileInputStream(“original.txt");FileOutputStreamout=newFileOutputStream(“result.txt");publicvoidwrite(intb)throwsIOExceptionpublicvoidwrite(byte[]b)throwsIOExceptionpublicvoidwrite(byte[]b,intoff,int

len)throwsIOException22字符流構(gòu)造方法publicFileReader(Filefile)throwsFileNotFoundExceptionpublicFileReader(String

fileName)throwsFileNotFoundExceptionpublicFile(Stringpathname)publicFileWriter(Filefile)throwsIOExceptionpublicFileWriter(Filefile,booleanappend)throwsIOExceptionpublicFileWriter(String

fileName)throwsIOExceptionpublicFileWriter(String

fileName,booleanappend)throwsIOException文件讀寫(xiě)操作23文件讀寫(xiě)-實(shí)例2文件讀寫(xiě)操作importjava.io.*;publicclassCopy{publicstaticvoidmain(String[]args)throwsIOException{FileinputFile=newFile(“original.txt");FileoutputFile=newFile(“result.txt");

FileReaderin=newFileReader(inputFile);

FileWriterout=newFileWriter(outputFile);

intc;while((c=in.read())!=-1)

out.write(c);

in.close();

out.close();}}FileReaderin=newFileReader(“original.txt");FileWriterout=newFileWriter(“result.txt");publicintread()throwsIOExceptionpublicintread(char[]cbuf)throwsIOExceptionpublicintread(char[]cbuf,intoff,int

len)throwsIOExceptionpublicvoidwrite(intb)throwsIOExceptionpublicvoidwrite(char[]cbuf)throwsIOExceptionpublicvoidwrite(char[]cbuf,intoff,int

len)throwsIOException24兩類緩沖流針對(duì)字節(jié)流java.io.BufferedInputStream類java.io.BufferedOutputStream類針對(duì)字符流java.io.BufferedReader類java.io.BufferedWriter類如何提高文件讀寫(xiě)效率25構(gòu)造方法publicBufferedInputStream(InputStreamin)2048bytespublicBufferedInputStream(InputStreamin,intsize)publicBufferedOutputStream(OutputStreamout)512bytespublicBufferedOutputStream(OutputStreamout,intsize)publicBufferedReader(Readerin)8192bytespublicBufferedReader(Readerin,int

sz)publicBufferedWriter(Writerout)8192bytepublicBufferedWriter(Writerout,int

sz)如何提高文件讀寫(xiě)效率26

Stringfilename=“test.txt”;

FileInputStream

fis=newFileInputStream(filename);

intcount=0;

intc;while((c=fis.read())!=-1){ if(c=='A'){ count++; }}

fis.close();

System.out.println(count);如何提高文件讀寫(xiě)效率-實(shí)例1如何提高文件讀寫(xiě)效率

Stringfilename=“test.txt”;

FileInputStream

fis=newFileInputStream(filename);

BufferedInputStream

bis=newBufferedInputStream(fis);

intcount=0;

intc;while((c=bis.read())!=-1){ if(c=='A'){ count++; }}

fis.close();

System.out.println(count);

FileInputStream

fis=newFileInputStream(“test.txt”);

intcount=0;finalintBUFSIZE=1024;bytebuf[]=newbyte[BUFSIZE];

int

len;while((len=fis.read(buf))!=-1){for(inti=0;i<len;i++){if(buf[i]=='A'){count++;}}}

fis.close();27

Stringfilename=“test.txt”;

FileReader

fr=newFileReader(filename);

BufferedReader

br=newBufferedReader(fr);

intcount=0;while(br.readLine()!=null){ count++;}

br.close();

System.out.println(count);如何提高文件讀寫(xiě)效率-實(shí)例2如何提高文件讀寫(xiě)效率28概述流的包裝(wrap)

FileReader

fr=newFileReader(filename);

BufferedReader

br=newBufferedReader(fr);

BufferedReader

br=newBufferedReader(newFileReader(filename));

PrintWriterout=newPrintWriter( newBufferedWriter(new

FileWriter(filename)));

publicStringreadLine()injava.io.BufferedReaderpublicvoidprintln(Stringx)injava.io.PrintWriter利用不同流的特點(diǎn)(方法)尋找合適的方法完成特定的需求對(duì)已有的流進(jìn)行再處理29流的種類節(jié)點(diǎn)流直接對(duì)數(shù)據(jù)源進(jìn)行讀/寫(xiě)操作的流處理流對(duì)一個(gè)已有的流進(jìn)行某種操作的流流的包裝(wrap)30節(jié)點(diǎn)流FileInputStream,PipedInputStream,ByteArrayInputStream,StringBufferInputStreamFileOutputStream,PipedOutputStream,ByteArrayOutputStreamCharArrayReader,FileReader,PipedReader,StringReaderCharArrayWriter,FileWriter,PipedWriter,StringWriter流的包裝(wrap)31處理流FilterOutputStream,DataOutputStream,BufferedOutputStream,PrintStreamFilterInputStream,LineNumberInputStream,DataInputStream,BufferedInputStream,PushbackInputStreamBufferedReader,LineNumberReader,InputStreamReader,FilterReader,PushBackReaderBufferedWriter,OutputStreamReader,FilterWriter,PrintWriter流的包裝(wrap)32一個(gè)實(shí)例java.io.FileReaderpublicFileReader(Filefile)throwsFileNotFoundExceptionpublicFileReader(String

fileName)throwsFileNotFoundExceptionjava.io.BufferedReaderpublicBufferedReader(Readerin)8192bytespublicBufferedReader(Readerin,int

sz)流的包裝改變了流的行為流的包裝(wrap)33流的包裝(wrap)-實(shí)例1流的包裝(wrap)importjava.io.*;publicclassEcho{publicstaticvoidmain(String[]args)throwsIOException{

BufferedReaderin=newBufferedReader( newInputStreamReader(System.in)); Strings;

while((s=in.readLine()).length()!=0)

System.out.println(s);}}publicstaticfinalInputStream

in(java.lang.System)publicInputStreamReader(InputStreamin)publicBufferedReader(Readerin)程序如何結(jié)束(跳出while循環(huán))?34概述java.io.DataInputStream類publicDataInputStream(InputStreamin)publicfinalboolean

readBoolean()publicfinalbytereadByte()publicfinalcharreadChar()publicfinalint

readInt()java.io.DataOutputStream類publicDataOutputStream(OutputStreamout)publicfinalvoidwriteBoolean(booleanv)publicfinalvoidwriteByte(intv)publicfinalvoidwriteChar(intv)publicfinalvoidwriteInt(intv)基本數(shù)據(jù)轉(zhuǎn)換流35DataOutputStreamout=newDataOutputStream(new

FileOutputStream("invoice1.txt"));doubleprices[]={19.99,9.99};intunits[]={12,8};Stringdescs[]={"JavaT-shirt","JavaMug"};for(inti=0;i<prices.length;i++){

out.writeDouble(prices[i]);

out.writeChar('\t');

out.writeInt(units[i]);

out.writeChar('\t');

out.writeChars(descs[i]);

out.writeChar('\n');}out.close();基本數(shù)據(jù)轉(zhuǎn)換流-實(shí)例基本數(shù)據(jù)轉(zhuǎn)換流DataInputStreamin=newDataInputStream( newFileInputStream("invoice1.txt"));doubletotal;try{ while(true){ doubleprice=in.readDouble();

in.readChar();

intunit=in.readInt();

in.readChar(); charchr;

StringBuffer

desc=newStringBuffer(20); while((chr=in.readChar())!=‘\n’)

desc.append(chr);

System.out.println("You'veordered"+unit+"units of"+desc+"at$"+price); total=total+unit*price; }}catch(EOFExceptione){}System.out.println("ForaTOTALof:$"+total);in.close();19.9912JavaT-shirt9.998JavaMug36概述java.io.File類:文件和目錄的路徑名構(gòu)造方法publicFile(Stringpathname)publicFile(Stringparent,Stringchild)publicFile(Fileparent,Stringchild)parent通常表示一個(gè)目錄,child則表示一個(gè)目錄或文件路徑名(pathname)UNIX平臺(tái)上絕對(duì)路徑的前綴“/”,相對(duì)路徑無(wú)前綴,例“/etc/inetd.conf”、“inetd.conf”Windows平臺(tái),絕對(duì)路徑名的前綴由“盤(pán)符:\”組成;UNC路徑名前綴為“\\”,然后是主機(jī)名和共享名,相對(duì)路徑名無(wú)盤(pán)符,例“c:\windows\notepad.exe”、“notepad.exe”目錄管理37方法publicboolean

canWrite()/canRead()publicbooleanexists()publicbooleandelete()刪除文件或目錄,若刪除目錄,要求該目錄必須為空publicboolean

createNewFile()創(chuàng)建一個(gè)空文件,當(dāng)且僅當(dāng)該文件不存在publicboolean

isDirectory()/isFile()/isHidden()publiclonglastModified()/publicboolean

setLastModified(longtime)publicString[]list()得到當(dāng)前目錄下的所有文件名和目錄名,若該File對(duì)象不是表示目錄,則返回nullpublicboolean

mkdir()/mkdirs()創(chuàng)建一個(gè)目錄publicboolean

renameTo(File

dest)publicboolean

setReadOnly()目錄管理38目錄管理—方法舉例目錄管理importjava.io.File;publicclassDirList{ publicstaticvoidmain(String[]args){ Filepath=newFile("."); String[]list=path.list();

for(inti=0;i<list.length;i++)

System.out.println(list[i]); }}39目錄管理—方法舉例目錄管理importjava.io.File;importjava.io.FilenameFilter;publicclassDirList{ publicstaticvoidmain(String[]args){ Filepath=newFile("."); String[]list=path.list(newDirFilter(args[0])); for(inti=0;i<list.length;i++)

System.out.println(list[i]); }}classDirFilterimplementsFilenameFilter{ Stringkey;

DirFilter(Stringkey){this.key=key;} publicboolean

accept(Filedir,Stringname){ Stringf=newFile(name).getName(); returnf.indexOf(key)!=-1; }}java.util.FilenameFilter

接口

publicboolean

accept(Filedir,Stringname);//是否指定的文件應(yīng)包含在文件列表中publicStringgetName()injava.io.File//獲得文件或目錄名,僅僅是最后的部分40隨機(jī)訪問(wèn)文件(RandomAccessFile)java.io.RandomAccessFile類讀寫(xiě)操作在同一個(gè)類中完成,須在構(gòu)造對(duì)象時(shí)指定參數(shù)通過(guò)移動(dòng)文件指針(filepointer)在文件的指定位置進(jìn)行讀寫(xiě)操作構(gòu)造方法publicRandomAccessFile(Stringname,Stringmode)throwsFileNotFoundExceptionpublicRandomAccessFile(Filefile,Stringmode)throwsFileNotFoundExceptionmode:“r”,“rw”隨機(jī)訪問(wèn)文件41方法publicvoidseek(longpos)publicintread()publicint

read(byte[]b)publicint

read(byte[]b,intoff,int

len)publicfinalboolean

readBoolean()/readByte()/readChar()/readShort()/readInt()/readDouble()/readFloat()publicfinalStringreadLine()publicvoidwrite(intb)publicvoidwrite(byte[]b)publicvoidwrite(byte[]b,intoff,int

len)publicfinalvoidwriteBoolean()/writeByte()/writeChar()/writeShort()/writeInt()/writeDouble()/writeFloat()隨機(jī)訪問(wèn)文件42隨機(jī)訪問(wèn)文件(RandomAccessFile)-實(shí)例隨機(jī)訪問(wèn)文件RandomAccessFile

rf=newRandomAccessFile("rtest.dat","rw");for(inti=0;i<10;i++)

rf.writeDouble(i*1.414);rf.close();rf=newRandomAccessFile("rtest.dat","rw");rf.seek(5*8);rf.writeDouble(47.0001);rf.close();rf=newRandomAccessFile("rtest.dat","r");for(inti=0;i<10;i++)

System.out.println("Value"+i+":"+rf.readDouble());rf.close();運(yùn)行結(jié)果:Value0:0.0Value1:1.414Value2:2.828Value3:4.242Value4:5.656Value5:47.0001Value6:8.484Value7:9.898Value8:11.312Value9:12.72599999999999943文件的特性讀和寫(xiě)的權(quán)限文件長(zhǎng)度修改時(shí)間是否是目錄java.io.File類設(shè)定文件屬性查詢文件屬性文件屬性44文件屬性獲取文件路徑importjava.io.*;classAttrDemo1{publicstaticvoidmain(String[]args)throwsIOException{Filetestfile=newFile("."+File.separatorChar+"testfile1");

testfile.createNewFile();

System.out.println("name="+testfile.getName());

System.out.println("path="+testfile.getPath());

System.out.println("absolutepath="+testfile.getAbsolutePath());

Syste

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論