7、chapter07文件和數(shù)據(jù)流_第1頁
7、chapter07文件和數(shù)據(jù)流_第2頁
7、chapter07文件和數(shù)據(jù)流_第3頁
7、chapter07文件和數(shù)據(jù)流_第4頁
7、chapter07文件和數(shù)據(jù)流_第5頁
已閱讀5頁,還剩67頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第七章 文件和數(shù)據(jù)流,浙江工業(yè)大學 計算機學院 趙小敏 59:9001,主要內(nèi)容,流的基本概念 字節(jié)流 字符流 文件類 隨機讀寫文件 對象序列化,7.1流的基本概念,數(shù)據(jù)流是從源到目的的字節(jié)的有序序列,先進先出。 兩種基本流:InputStream(輸入流)和OutputStream(輸出流),Java的標準輸入輸出,標準輸入輸出是指在命令行方式下的輸入輸出方式。 Java通過System.in、System.out和System.err來實現(xiàn)標準輸入輸出和標準錯誤輸出。 每當main方法被執(zhí)行時,就自動生成System.in、System.out和Syst

2、em.err三個對象。,Java的標準輸入輸出,System.in是字節(jié)輸入流InputStream類的一個對象,其中有read方法從鍵盤讀入數(shù)據(jù): public int read() throws IOException public int read(byte b) throws IOException System.out是流PrintStream類的一個對象,其中print和println方法向屏幕輸出數(shù)據(jù)。 System.err是流PrintStream類的一個對象,用于向屏幕輸出錯誤信息。,例1:輸入輸出的實例,public class Stdin_out public stati

3、c void main(String args) byte buffer =new byte200; int i,d=0,count=0; System.out.print(Input a string: ); try count=System.in.read(buffer); catch(Exception e) System.err.println(發(fā)生異常:+ e.toString(); for(i=0;i=count-1;i+) System.out.print(char)bufferi); System.out.println(count); System.out.println(I

4、nput ten char: ); for(i=1;i=10;i+) try d=System.in.read(); System.out.println(char)d); catch(Exception e) System.err.println(發(fā)生異常:+e.toString(); ,Java的數(shù)據(jù)流,Java的數(shù)據(jù)流都在java.io包里 Java的數(shù)據(jù)流根據(jù)操作的數(shù)據(jù)流分為字節(jié)流和字符流 字節(jié)流:流中的數(shù)據(jù)以8位字節(jié)為單位進行讀寫,以InputStream和OutputStream為基礎(chǔ)類。 字符流:流中的數(shù)據(jù)以16位字節(jié)為單位進行讀寫,以Reader和Writer為基礎(chǔ)類。,7.2

5、字節(jié)流,InputStream和OutputStream分別是字節(jié)輸入流和字節(jié)輸出流的超類 InputStream和OutputStream提供許多用于字節(jié)輸入輸出的方法,包括: 數(shù)據(jù)的讀取 數(shù)據(jù)的寫入 標記位置 獲取數(shù)據(jù)量 關(guān)閉數(shù)據(jù)流,字節(jié)輸入流InputStream類的層次結(jié)構(gòu),InputStream 方法,三個基本read()方法 int read()/讀一個字節(jié)返回 int read(byte ) / 將數(shù)據(jù)讀入byte, 返回讀的字節(jié)數(shù) int read( byte, int offset, int length ) 其它方法 void close( ) /關(guān)閉流。自頂向下關(guān)閉Fil

6、ter stream int available() /返回未讀的字節(jié)數(shù) long skip(long n) / 跳過n個字節(jié) boolean markSupported( ) /測試打開的流是否支持書簽 void mark(int) /標記當前流,并建立int大小緩沖區(qū) void reset( ) / 返回標簽出,字節(jié)輸出流OutputStream類層次,OutputStream方法,三個基本的write( )方法 void write( int ) / 寫一個字節(jié) void write(byte ) / 寫一個字節(jié)數(shù)組 void write(byte , int offset, int l

7、ength ) 其它方法 void close( ) void flush( ) / 強行寫,字節(jié)文件輸入輸出流:FileInputStream和FileOutputStream,FileInputStream和FileOutputStream實現(xiàn)了對文件的順序訪問,以字節(jié)為單位對文件進行讀寫操作,主要有這樣幾步: 創(chuàng)建文件輸入輸出流的對象 用文件讀寫方法讀寫數(shù)據(jù) 關(guān)閉數(shù)據(jù)流。,1、創(chuàng)建文件輸入輸出流對象,(1)創(chuàng)建FileInputStream的對象,打開要讀取數(shù)據(jù)的文件 FileInputStream的構(gòu)造方法是: public FileInputStream(String name) t

8、hrows FileNotFoundException public FileInputStream(File file) throws FileNotFoundException 如下面語句可以創(chuàng)建文件的輸入流對象,并打開要讀取數(shù)據(jù)的文件D:/java/temp/mytext.txt : FileInputStream rf=new FileInputStream(“D:/java/temp/mytext.txt ”);,1、創(chuàng)建文件輸入輸出流對象,(2)創(chuàng)建FileOutputStream的對象,打開要寫入數(shù)據(jù)的文件 FileOutputStream的構(gòu)造方法是: public FileO

9、utputStream(String name) throws FileNotFoundException public FileOutputStream(String name,boolean append) throws FileNotFoundException public FileOutputStream(File file) throws FileNotFoundException 其中:name是要打開的文件名,file是文件類File的對象。如下面語句可以創(chuàng)建文件的輸出流對象,并打開要寫入數(shù)據(jù)的文件D:/java/temp/mytext.txt : FileOutputStre

10、am wf=new FileOutputStream(“D:/java/temp/mytext.txt ”);,2、對文件進行讀寫的方法,(1)用read方法讀取文件的數(shù)據(jù) public int read( ) throws IOException /返回從文件中讀取的一個字節(jié)。 public int read(byte b) throws IOException public int read(byte b,int off,int len) throws IOException /返回讀取的字節(jié)數(shù),若b的長度為0,返回0。,2、對文件進行讀寫的方法,(2)用write方法將數(shù)據(jù)寫入文件 pu

11、blic void write(int b) throws IOException /向文件寫入一個字節(jié),b是int類型,所以將b的低8位寫入 public void write(byte b) throws IOException public void write(byte b,int off,int len) throws IOException /將字節(jié)數(shù)組寫入文件,其中off是b中的起始位置,len是寫入的最大長度。,3、字節(jié)文件流的關(guān)閉,當讀寫操作完畢時,要關(guān)閉輸入或輸出流,釋放相關(guān)的系統(tǒng)資源。 如果發(fā)生I/O錯誤,拋出IOException異常。 關(guān)閉數(shù)據(jù)流的方法是: publi

12、c void close( ) throws IOException,例2:讀取文件內(nèi)容并顯示在屏幕,import java.io.*; public class FileIn public static void main(String args) try FileInputStream rf=new FileInputStream( H:/java/temp/mytext.txt); int b; while(b=rf.read()!=-1) System.out.print(char)b); rf.close(); catch(IOException ie) System.out.pri

13、ntln(ie); catch(Exception e) System.out.println(e); ,例3:復制文件,import java.io.*; public class FileIn_Out public static void main(String args) try FileInputStream rf=new FileInputStream(D:/java/temp/file1.txt); FileOutputStream wf=new FileOutputStream(D:/java/temp/file2.txt); byte b=new byte512; int co

14、unt=-1; while(count=rf.read(b,0,512)!=-1) wf.write(b,0,count); rf.close(); wf.close(); catch(IOException ie) System.out.println(ie.toString(); catch(Exception e) System.out.println(e.toString(); ,7.3字符流,類Reader是字符輸入流的抽象超類,其提供的方法與InputStream類似,只是將基于Byte的參數(shù)改為基于Char。 類Writer是字符輸出流的抽象超類,其提供的方法與OutputStr

15、eam類似,只是將基于Byte的參數(shù)改為基于Char。 Reader和Writer 類實現(xiàn)字節(jié)和字符間的自動轉(zhuǎn)換。 每一個核心輸入、輸出流,都有相應的Reader和Writer版本。,Reader的類層次結(jié)構(gòu),Reader的基本方法,int read();/讀單個字符 int read(char cbuf);/讀字符放入數(shù)組中 int read(char cbuf, int offset, int length);/讀字符放入數(shù)組的指定位置 void close( ) /關(guān)閉流。 long skip(long n) / 跳過n個字符 boolean markSupported( ) /測試打開

16、的流是否支持書簽 void mark(int) /標記當前流,并建立int大小緩沖區(qū) void reset( ) / 返回標簽出 boolean ready() /測試當前流是否準備好進行讀,Writer的類層次結(jié)構(gòu),Writer的基本方法,int write(int c) ; / 寫單個字符 int write(char cbuf) ;/ 寫字符數(shù)組 int write(char cbuf, int offset, int length) ; int write(String str) ; int write(String str, int offset, int length) ; voi

17、d close( ) void flush( ) / 強行寫,字符文件輸入輸出流:FileReader和FileWrite,FileReader和Filewriter類用于字符文件的輸入和輸出 讀寫文件的過程: 先創(chuàng)建對象打開文件 然后用讀寫方法從文件中讀取數(shù)據(jù)或?qū)?shù)據(jù)寫入文件 最后關(guān)閉數(shù)據(jù)流。,1、創(chuàng)建字符流文件對象,打開文件,創(chuàng)建FileReader或Filewriter對象,打開要讀寫的文件 FileReader的構(gòu)造方法: public FileReader(String filename) public FileReader(File file) FileWriter的構(gòu)造方法: p

18、ublic FlieWriter(String filename) public Filewriter(File file),2、字符文件流的讀寫,用從超類繼承的read和write方法可以對打開的文件進行讀寫 讀取文件數(shù)據(jù)的方法: int read( ) throws IOException int read(char b ) throws IOException int read(char b ,int off,int len) throws IOException 數(shù)據(jù)寫入到文件的方法: void write(char b) throws IOException void write(c

19、har b ) throws IOException void write(char b ,int off,int len) throws IOException,3、字符文件流的關(guān)閉,對文件操作完畢要用close方法關(guān)閉數(shù)據(jù)流。 public void close( ) throws IOException,例5:從鍵盤輸入一行文字,寫入文件file3.txt中,import java.io.*; public class FilecharOut public static void main(String args) char c=new char512; byte b=new byte5

20、12; int n,i; try FileWriter wf=new FileWriter(file3.txt); n=System.in.read(b); for(i=0;in;i+) ci=(char)bi; wf.write(c); wf.close(); catch(IOException e) System.out.println(e); ,字符緩沖流: BufferedReader和BufferedWriter,BufferedReader和BufferedWriter類以緩沖區(qū)方式對數(shù)據(jù)進行輸入輸出。 1.BufferedReader用于字符緩沖輸入,構(gòu)造方法如下: public

21、 BufferedReader(Reader in) public BufferedReader(Reader in,int sz) 其中:in為超類Reader的對象,sz為用戶設定的緩沖區(qū)大小。,2. BufferedWriter類,Bufferedwriter用于字符緩沖流輸出,構(gòu)造方法為: public BufferedWriter(Writer out) public Bufferedwriter(Writer out,int sz) 其中:out為超類Writer的對象,sz為用戶設定的緩沖區(qū)大小。,例6:從鍵盤輸入文字存入文件,再讀出加上行號后打印在屏幕,import java.

22、io.*; public class BufferDemo public static void main(String args) String f=f.txt; String str=; int i=0; try BufferedReader keyIn=new BufferedReader(new InputStreamReader(System.in); BufferedWriter bw=new BufferedWriter(new FileWriter(f); BufferedReader br = new BufferedReader(new FileReader(f); Sys

23、tem.out.println(Please input file text:); while(!(str=keyIn.readLine().equals(exit) bw.write(str,0,str.length(); bw.newLine(); bw.close(); while(str=br.readLine()!=null) i+; System.out.println(i+: +str); catch(IOException e) ,字節(jié)流與字符流的比較,Reader 和 InputStream以及Writer 與 OutputStream定義的API類似,但操作的數(shù)據(jù)類型不同。

24、 所有的流InputStream、 OutputStream 、Reader、 Writer 在創(chuàng)建時自動打開;程序中可以調(diào)用close方法關(guān)閉流,否則Java運行環(huán)境的垃圾收集器將隱含將流關(guān)閉。,7.4文件類,創(chuàng)建文件流:常用文件名或File類的對象創(chuàng)建文件流。 文件過濾:將符合條件的文件選擇出來進行操作,通過接口FileFilter和FilenameFilter來實現(xiàn)。,文件類File,提供對文件進行創(chuàng)建目錄、創(chuàng)建臨時文件、改變文件名、刪除文件等操作 提供獲取文件信息的方法,如文件名、文件路徑和文件長度等 File類的構(gòu)造方法: public File(String pathname) p

25、ublic File(String parent,String child) public File(File parent,String child) public File(URI uri),File類的方法,1 、訪問文件對象 public String getName( ) public String getPath( ) public String getAbsolutePath( ) public String getParent( ) public File getParentFile( ) 2、文件操作 public boolean renameTo(File dest) pu

26、blic boolean delete(),File類的方法(續(xù)),3、獲得文件的屬性 public long length() public boolean exists() public long lastMoidfied() 4、目錄操作 public boolean mkdir() public String list() public File listFiles();,例8:文件類的使用,import java.io.*; import java.util.*; public class File_ex void FileInformation(File f) System.out

27、.println(f.getName(); System.out.println(f.getAbsolutePath(); System.out.println(f.getParent(); System.out.println(f.length(); System.out.println(new Date(f.lastModified(); void DirectoryInformation(File d) System.out.println(d.getName(); System.out.println(d.getParent(); int i=0; String lt=d.list()

28、; while(ilt.length) System.out.println(lti); i+; ,例8:文件類的使用(續(xù)),public static void main(String args) File f=new File(D:/java/2007-2Java/code/ch08,file1.txt); File d=new File(D:/java/2007-2Java/code/ch08/data2); File d1=new File(D:/java/2007-2Java/code/ch08/data2/data3); File_ex fe=new File_ex(); fe.F

29、ileInformation(f); fe.DirectoryInformation(d); d1.mkdir(); ,練習1:閱讀下面的程序,寫出帶劃線語句或注釋,并寫出該程序的作用。,import java.io.*; public class Test public static void main(String args) scanFiles(c:/); public static void scanFiles(String path) if (path = null) return; File f = new File(path); /_ if (!f.exists() return

30、; if (f.isFile() /_ System.out.println(f.getAbsolutePath(); else File dir = f.listFiles(); for (int i = 0; i dir.length; i+) scanFiles(diri.getAbsolutePath();/_ ,練習2:打印某目錄下(包含子目錄)所有文件的規(guī)范路徑名和文件大小,import java.io.*; public class File_Size public static void main(String args)throws IOException File file

31、s=new File(.); listPath(files); public static void listPath(File f)throws IOException String file_list=f.list(); for(int i=0;ifile_list.length;i+) File current_file=new File(f.getPath(),file_listi); if(current_file.isDirectory() listPath(current_file); if(current_file.isFile() try System.out.println

32、(current_file.getCanonicalPath()+:+current_file.length(); catch(IOException e) e.printStackTrace(); /if /for ,文件過濾接口FileFilter和FilenameFilter,這兩個接口中有方法accept,接口的說明如下: public interface FileFilter public boolean accept(File pathname); /參數(shù)pathname是要過濾目錄中的文件對象。 public interface FilenameFilter public boo

33、lean accept(File dir, String name); /參數(shù)dir是要過濾的目錄,name是目錄中的文件名,過濾功能的使用,要實現(xiàn)過濾的功能,就要聲明一個類實現(xiàn)FileFilter和FilenameFilter接口中的方法。 在使用File類的list和listFiles方法時,以一個過濾器對象作為參數(shù),就可實現(xiàn)對文件名的過濾。 public String list(FilenameFilter filter) public File listFiles(FilenameFilter filter) public File listFlies(FileFilter filte

34、r),例9:顯示C:windows目錄下.exe文件。,import java.io.*; class ListFilter implements FilenameFilter private String pre=,ext=; public ListFilter(String filterstr) int i,j; filterstr=filterstr.toLowerCase(); i=filterstr.indexOf(*); j=filterstr.indexOf(.); if(i0) pre=filterstr.substring(0,i); if(i=-1 ,例9:顯示C:wind

35、ows目錄下.exe文件。,public boolean accept(File dir,String filename) boolean y=true; try filename=filename.toLowerCase(); y=filename.startsWith(pre) ,7.5 文件的隨機讀寫,在文件的任意位置讀或?qū)憯?shù)據(jù),而且可以同時進行讀和寫的操作。 RandomAccessFile類提供的對文件隨機訪問方式。 RandomAccessFile的構(gòu)造方法 public RandomAccessFile(File file,String mode) throws FileNotf

36、oundException public RandomAccessFile(String name,String mode) throws FileNotfoundException file和name是文件對象和文件名字符串。 mode是對訪問方式的設定:r表示讀,w表示寫,rw表示讀寫,RandomAccessFile的方法,public long length( ) 返回文件的長度 public void seek(long pos) 改變文件指針的位置 public final int readInt( )讀一個整型數(shù)據(jù) public final void writeInt(int v

37、) 寫入一個整型數(shù)據(jù) public long getFilePointer( ) 返回文件指針的位置 public void close( ) 關(guān)閉文件,例10:隨機訪問文件的演示程序,import java.io.IOException; import java.io.RandomAccessFile; public class RandomAccessFileDemo public static void main(String args ) try RandomAccessFile f=new RandomAccessFile(test.txt, rw); int i; double d

38、; for (i=0; i10; i+) f.writeDouble(Math.PI*i); f.seek(16); f.writeDouble(0); f.seek(0); for (i=0; i 10; i+) d=f.readDouble( ); System.out.println( + i + : + d); f.close( ); catch (IOException e) System.err.println(發(fā)生異常: + e); e.printStackTrace( ); ,7.6 對象序列化,序列化的過程就是對象寫入字節(jié)流和從字節(jié)流中讀取對象。將對象狀態(tài)轉(zhuǎn)換成字節(jié)流之后,可

39、以用java.io包中的各種字節(jié)流類將其保存到文件中,管道到另一線程中或通過網(wǎng)絡連接將對象數(shù)據(jù)發(fā)送到另一主機。 對象序列化的典型應用 在RMI、Socket、JMS、EJB都有應用 程序版本升級問題,序列化機制,序列化分為兩部分:序列化和反序列化。 序列化是將數(shù)據(jù)分解成字節(jié)流,以便存儲在文件中或在網(wǎng)絡上傳輸。 反序列化是打開字節(jié)流并重構(gòu)對象。 對象序列化不僅要將基本數(shù)據(jù)類型轉(zhuǎn)換成字節(jié)表示,有時還要恢復數(shù)據(jù)。恢復數(shù)據(jù)要求有恢復數(shù)據(jù)的對象實例。,定制對象序列化,實現(xiàn)java.io.Serializable接口的類對象可以轉(zhuǎn)換成字節(jié)流或從字節(jié)流恢復。 public class Serializabl

40、eClass implements Serializable String today=Today:; transient Date todayDate=new Date(); 只有對象的數(shù)據(jù)被保存,方法與構(gòu)造函數(shù)不被序列化。 聲明為transient或static的變量不能被序列化。,處理對象流(序列化過程和反序列化過程),java.io.ObjectOutputStream負責將對象寫入字節(jié)流 java.io.ObjectInputStream從字節(jié)流重構(gòu)對象。,序列化過程:序列化todaysdate到一個文件中,FileOutputStream f = new FileOutputStr

41、eam(tmp); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject(Today); s.writeObject(new Date(); s.flush();,反序列化過程:從文件中反序列化String對象和Date對象,FileInputStream in = new FileInputStream(tmp); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Da

42、te)s.readObject();,例:將Student對象序列化,import java.io.Serializable; public class Student implements Serializable static final long serialVersionUID = 123456L; String m_name; int m_id; int m_height; /int m_weight; public Student( String name, int id, int h ) m_name = name; m_id = id; m_height = h; public

43、 void output( ) System.out.println(姓名: + m_name); System.out.println(學號: + m_id); System.out.println(身高: + m_height); ,例:將Student對象數(shù)據(jù)寫入object.dat,import java.io.FileOutputStream; import java.io.ObjectOutputStream; public class WriteObject public static void main(String args ) try ObjectOutputStream

44、f = new ObjectOutputStream( new FileOutputStream(object.dat); Student s = new Student( 張三, 2003001, 172); f.writeObject(s); s.output( ); f.close( ); catch (Exception e) System.err.println(發(fā)生異常: + e); e.printStackTrace( ); ,例:從object.dat讀出Student對象數(shù)據(jù),import java.io.FileInputStream; import java.io.Obj

45、ectInputStream; public class ReadObject public static void main(String args ) try ObjectInputStream f = new ObjectInputStream( new FileInputStream(object.dat); Student s = (Student)(f.readObject( ); s.output( ); f.close( ); catch (Exception e) System.err.println(發(fā)生異常: + e); e.printStackTrace( ); ,im

46、port java.io.*; public class Test public static void main(String argv) ; /創(chuàng)建Test對象,對象名為t System.out.println(t.fliton(); public int fliton() try /第10行的含義是: FileInputStream din = new FileInputStream(test.txt); din.read(); catch(IOException ioe) /第12行的含義是: System.out.println(one); return -1; finally Sy

47、stem.out.println(two); return 0; 如果文件test.txt與Test.java在同一個目錄下,test.txt中僅有一行字符串“hello world!”,運行結(jié)果是什么?,練習1:閱讀下面的程序Test.java,先填寫空格的內(nèi)容,然后寫出運行結(jié)果:,練習2:文件拷貝,編寫一個拷貝任意類型文件的類程序CopyFile.java,該類就只有一個方法copy(),方法聲明如下: public boolean copy(String fromFileName, String toFileName,boolean override) 其中,參數(shù)1:fromFileNa

48、me 源文件名;參數(shù)2:toFileName 目標文件名;參數(shù)3: override 目標文件存在時是否覆蓋。若文件拷貝成功,則copy()方法返回true,否則返回false。,class CopyFile public boolean copy(String fromFileName, String toFileName,boolean override) File fromFile = new File(fromFileName); File toFile = new File(toFileName); if (!fromFile.exists() | !fromFile.isFile(

49、) | !fromFile.canRead() return false; if (toFile.isDirectory() toFile = new File(toFile, fromFile.getName(); if (toFile.exists() if (!toFile.canWrite() | override = false) return false; else String parent = toFile.getParent(); if (parent = null) parent = System.getProperty(user.dir); File dir = new

50、File(parent); if (!dir.exists() | dir.isFile() | !dir.canWrite() return false; ,FileInputStream from = null; FileOutputStream to = null; try from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte buffer = new byte4096; int bytes_read; while ( (bytes_read = from.read(buffer) !=

51、 -1) to.write(buffer, 0, bytes_read); return true; catch (IOException e) return false; ,finally if (from != null) try from.close(); catch (IOException e) if (to != null) try to.close(); catch (IOException e) ,練習3,編寫一個程序,統(tǒng)計給定文件中包含的每個單詞出現(xiàn)的頻率,并按單詞表的順序顯示統(tǒng)計結(jié)果。,import java.util.*; import java.io.*; public

52、 class testWordCount private String word; private int wordNum; public testWordCount(String wordStr,int num) word=wordStr; wordNum=num; public static void main(String args) String fileName=WordCount.txt; try BufferedReader br = new BufferedReader(new FileReader(fileName); String line = br.readLine();

53、 StringBuffer filecontent=new StringBuffer(); while(line!=null) filecontent.append(line); line = br.readLine(); WordCount(filecontent.toString(); br.close(); catch (Exception ex) ex.printStackTrace(); ,static void WordCount(String filename) String str=filename; String str1=str.replaceAll(a-zA-Z, );

54、StringTokenizer st=new StringTokenizer(str1, ); int j=st.countTokens(); ArrayList wordcount=new ArrayList(); for(int i=0;ij;i+) wordcount=wordSort(wordcount,st.nextToken().toLowerCase(); /output wordCount results for(int i=0;iwordcount.size();i+) System.out.println(testWordCount)wordcount.get(i).wor

55、d +: +(testWordCount)wordcount.get(i).wordNum); static String readFileContent(String filename) StringBuffer buf = new StringBuffer(); try BufferedReader br = new BufferedReader(new FileReader(filename); String line = br.readLine(); while(line!=null) buf.append(line); br.close(); catch (Exception ex)

56、 ex.printStackTrace(); /catch return buf.toString(); ,static ArrayList wordSort(ArrayList a,String aWord) testWordCount wordArray=new testWordCount(aWord,1); if(a.size()=0;i-) int flag=aWpareTo(testWordCount)a.get(i).word); if(flag=0) wordArray.wordNum=(testWordCount)a.get(i).wordNum+1; wordArray.word=aWord; a.set(i,wordArray); break; /if(flag=0) if(flag0) a.add(i+1,wordArray); break; /if(flag0) if(flag0 /ArrayList wordSort ,小結(jié),復制文件,可用字節(jié)輸入輸出流FileInputStream和FileOutputStream實現(xiàn),見列3 讀寫文件內(nèi)容,可用BufferedReader和BufferedWriter實現(xiàn),見例6

溫馨提示

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

評論

0/150

提交評論