Java語(yǔ)言程序設(shè)計(jì)(文件輸入輸出、Java網(wǎng)絡(luò)編程)課件_第1頁(yè)
Java語(yǔ)言程序設(shè)計(jì)(文件輸入輸出、Java網(wǎng)絡(luò)編程)課件_第2頁(yè)
Java語(yǔ)言程序設(shè)計(jì)(文件輸入輸出、Java網(wǎng)絡(luò)編程)課件_第3頁(yè)
Java語(yǔ)言程序設(shè)計(jì)(文件輸入輸出、Java網(wǎng)絡(luò)編程)課件_第4頁(yè)
Java語(yǔ)言程序設(shè)計(jì)(文件輸入輸出、Java網(wǎng)絡(luò)編程)課件_第5頁(yè)
已閱讀5頁(yè),還剩78頁(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)介

1、Java語(yǔ)言程序設(shè)計(jì)馬 皓1第1頁(yè),共83頁(yè)。第九章 文件輸入輸出概念Java I/O類Java I/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性2第2頁(yè),共83頁(yè)。概念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第3頁(yè),共83頁(yè)。概念讀/寫(xiě)流的一般流程讀(Reading)open a stream /打開(kāi)讀出流while more information /判斷

2、 read information /讀close the stream /關(guān)閉流寫(xiě)(Writing)open a stream /打開(kāi)寫(xiě)入流while more information /判斷 write information /寫(xiě)close the stream /關(guān)閉流4第4頁(yè),共83頁(yè)。概念兩種流的定義(讀取信息的基本數(shù)據(jù)單位)字節(jié)流(byte stream): 一個(gè)字節(jié)(8-bit)一個(gè)字節(jié)讀/寫(xiě)字符流(character stream):一個(gè)字符一個(gè)字符讀/寫(xiě)(具有特定字符編碼的數(shù)據(jù))j a v a 語(yǔ) 言6A 61 76 61 D3 EF D1 D4以字節(jié)流的方式讀: 讀8次,

3、8個(gè)字節(jié)以字符流的方式讀: 讀6次,6個(gè)字符5第5頁(yè),共83頁(yè)。第九章 文件輸入輸出概念Java I/O類Java I/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性6第6頁(yè),共83頁(yè)。Java I/O類字節(jié)流的讀/寫(xiě)操作(來(lái)自JDK1.0)java.io.InputStream (抽象類)public abstract int read()public int read(byte b)public int read(byte b, int offset, int length)到達(dá)流的終點(diǎn),無(wú)數(shù)據(jù)讀出則返回-1java.io.OutputStream (抽象類)public a

4、bstract void write(int b)public void write(byte b)public void write(byte b, int offset, int length) 所有的讀/寫(xiě)函數(shù)都拋出java.io.IOException7第7頁(yè),共83頁(yè)。Java I/O類字符流的讀/寫(xiě)操作(來(lái)自JDK1.1)java.io.Reader (抽象類)public int read()public int read(char cbuf)publc abstract int read(char cbuf, int offset, int length)到達(dá)流的終點(diǎn),無(wú)數(shù)據(jù)讀

5、出則返回-1java.io.Writer (抽象類)public void write(int c)public void write(char cbuf)public void write(char cbuf, int offset, int length)所有的讀/寫(xiě)函數(shù)都拋出java.io.IOException8第8頁(yè),共83頁(yè)。Java I/O類I/O流的層次關(guān)系class java.io.InputStream的子類class java.io.ByteArrayInputStreamclass java.io.FileInputStreamclass java.io.FilterI

6、nputStreamclass java.io.BufferedInputStreamclass java.io.DataInputStream class java.io.LineNumberInputStreamclass java.io.PushbackInputStreamclass java.io.ObjectInputStream class java.io.PipedInputStreamclass java.io.SequenceInputStreamclass java.io.StringBufferInputStream 9第9頁(yè),共83頁(yè)。Java I/O類I/O流的層次

7、關(guān)系class java.io.OutputStream的子類class java.io.ByteArrayOutputStreamclass java.io.FileOutputStreamclass java.io.FilterOutputStreamclass java.io.BufferedOutputStreamclass java.io.DataOutputStream class java.io.PrintStreamclass java.io.ObjectOutputStreamclass java.io.PipedOutputStream 10第10頁(yè),共83頁(yè)。Java I

8、/O類I/O流的層次關(guān)系class java.io.Reader的子類class java.io.BufferedReaderclass java.io.LineNumberReaderclass java.io.CharArrayReaderclass java.io.FilterReader class java.io.PushbackReaderclass java.io.InputStreamReaderclass java.io.FileReaderclass java.io.PipedReaderclass java.io.StringReader 11第11頁(yè),共83頁(yè)。Java

9、 I/O類I/O流的層次關(guān)系class java.io.Writer的子類class java.io.BufferedWriterclass java.io.CharArrayWriterclass java.io.FilterWriterclass java.io.OutputStreamWriter class java.io.FileWriterclass java.io.PipedWriterclass java.io.PrintWriterclass java.io.StringWriter 12第12頁(yè),共83頁(yè)。Java I/O類I/O流的分類 (12個(gè)功能類)I/O類型所用到的

10、類描述文件FileReader/FileWriterFileInputStream/FileOutputStream文件流(讀/寫(xiě)文件)緩沖BufferedReader/BufferedWriterBufferedInputStreamBufferedOutputStream提高讀/寫(xiě)效率打印PrintWriter PrintStream (System.out.println()內(nèi)存CharArrayReader/CharArrayWriterByteArrayInputStreamByteArrayOutputStreamStringReader/StringWriterStringBuf

11、ferInputStream讀/寫(xiě)內(nèi)存13第13頁(yè),共83頁(yè)。Java I/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ì)象串行化ObjectInputStreamO

12、bjectOutputStreamObject Serialization14第14頁(yè),共83頁(yè)。Java I/O類I/O流的分類 (12個(gè)功能類)I/O類型所用到的類描述基本數(shù)據(jù)轉(zhuǎn)化DataInputStreamDataOutputStreamJAVA基本數(shù)據(jù)類型的讀/寫(xiě)計(jì)數(shù)LineNumberReaderLineNumberInputStream讀操作時(shí)記錄行數(shù)Peeking AheadPushbackReaderPushbackInputStream可回退緩存(pushback buffer)過(guò)濾FilterReader/FilterWriterFilterInputStream/Fil

13、terOutputStream在讀/寫(xiě)操作時(shí)對(duì)數(shù)據(jù)進(jìn)行過(guò)濾15第15頁(yè),共83頁(yè)。第九章 文件輸入輸出概念Java I/O類Java I/O操作標(biāo)準(zhǔn)輸入/輸出文件讀寫(xiě)操作目錄管理隨機(jī)訪問(wèn)文件文件屬性16第16頁(yè),共83頁(yè)。Java I/O操作主要內(nèi)容標(biāo)準(zhǔn)輸入/輸出控制臺(tái)屏幕打印和鍵盤(pán)讀入文件I/O操作文件讀寫(xiě)如何提高文件讀寫(xiě)效率流的包裝(Wrap)基本數(shù)據(jù)轉(zhuǎn)換流目錄管理隨機(jī)訪問(wèn)文件(Random Access File)文件屬性網(wǎng)絡(luò)流操作(見(jiàn)第十三講)17第17頁(yè),共83頁(yè)。輸出: 控制臺(tái)屏幕打印class Test public static void main(String args) S

14、ystem.out.println(“Hello World!”);標(biāo)準(zhǔn)輸入/輸出18第18頁(yè),共83頁(yè)。輸入: 鍵盤(pán)讀入import java.io.IOException;class Test public static void main(String args) throws IOException byte b = new byte10;System.out.println(Received number= + System.in.read(b);標(biāo)準(zhǔn)輸入/輸出C:java TestAReceived number=3C:19第19頁(yè),共83頁(yè)。文件讀/寫(xiě)流程打開(kāi)文件流條件判斷讀出/

15、寫(xiě)入關(guān)閉文件流兩種類型文件FileInputStream/FileOutputStream (字節(jié)流)FileReader/FileWriter (字符流)文件讀寫(xiě)操作20第20頁(yè),共83頁(yè)。字節(jié)流構(gòu)造方法public FileInputStream(File file) throws FileNotFoundExceptionpublic FileInputStream(String name) throws FileNotFoundExceptionpublic File(String pathname)public FileOutputStream(File file) throws F

16、ileNotFoundExceptionpublic FileOutputStream(File file, boolean append) throws FileNotFoundException /是否向已存在的文件后添加public FileOutputStream(String name) throws FileNotFoundExceptionpublic FileOutputStream(String name, boolean append) throws FileNotFoundException文件讀寫(xiě)操作21第21頁(yè),共83頁(yè)。文件讀寫(xiě)-實(shí)例1文件讀寫(xiě)操作import ja

17、va.io.*;public class CopyBytes public static void main(String args) throws IOException File inputFile = new File(“original.txt); File outputFile = new File(“result.txt); FileInputStream in = new FileInputStream(inputFile); FileOutputStream out = new FileOutputStream(outputFile); int c; while (c = in

18、.read() != -1) out.write(c); in.close(); out.close(); public int read ()throws IOExceptionpublic int read (byte b) throws IOExceptionpublic int read (byte b, int off, int len) throws IOExceptionFileInputStream in = new FileInputStream(“original.txt);FileOutputStream out = new FileOutputStream(“resul

19、t.txt);public void write (int b) throws IOExceptionpublic void write (byte b) throws IOExceptionpublic void write (byte b, int off, int len) throws IOException22第22頁(yè),共83頁(yè)。字符流構(gòu)造方法public FileReader(File file) throws FileNotFoundExceptionpublic FileReader(String fileName) throws FileNotFoundExceptionpu

20、blic File(String pathname)public FileWriter(File file) throws IOExceptionpublic FileWriter(File file, boolean append) throws IOExceptionpublic FileWriter(String fileName) throws IOExceptionpublic FileWriter(String fileName, boolean append) throws IOException文件讀寫(xiě)操作23第23頁(yè),共83頁(yè)。文件讀寫(xiě)-實(shí)例2文件讀寫(xiě)操作import jav

21、a.io.*;public class Copy public static void main(String args) throws IOException File inputFile = new File(“original.txt); File outputFile = new File(“result.txt); FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); int c; while (c = in.read() != -1) out.write(c);

22、 in.close(); out.close(); FileReader in = new FileReader(“original.txt);FileWriter out = new FileWriter(“result.txt);public int read () throws IOExceptionpublic int read (char cbuf) throws IOExceptionpublic int read (char cbuf, int off, int len) throws IOExceptionpublic void write (int b) throws IOE

23、xceptionpublic void write (char cbuf) throws IOExceptionpublic void write (char cbuf, int off, int len) throws IOException24第24頁(yè),共83頁(yè)。兩類緩沖流針對(duì)字節(jié)流java.io.BufferedInputStream類java.io.BufferedOutputStream類針對(duì)字符流java.io.BufferedReader類java.io.BufferedWriter類如何提高文件讀寫(xiě)效率25第25頁(yè),共83頁(yè)。構(gòu)造方法public BufferedInputSt

24、ream(InputStream in) 2048 bytespublic BufferedInputStream(InputStream in, int size)public BufferedOutputStream(OutputStream out) 512 bytespublic BufferedOutputStream(OutputStream out, int size)public BufferedReader(Reader in) 8192 bytespublic BufferedReader(Reader in, int sz)public BufferedWriter(Wr

25、iter out) 8192 bytepublic BufferedWriter(Writer out, int sz)如何提高文件讀寫(xiě)效率26第26頁(yè),共83頁(yè)。 String filename = “test.txt”; FileInputStream fis = new FileInputStream(filename); int count = 0; int c; while (c = fis.read() != -1) if (c = A) count+; fis.close(); System.out.println(count);如何提高文件讀寫(xiě)效率-實(shí)例1如何提高文件讀寫(xiě)效率

26、String filename = “test.txt”; FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); int count = 0; int c; while (c = bis.read() != -1) if (c = A) count+; fis.close(); System.out.println(count); FileInputStream fis = new FileInputStream(“test.txt

27、”); int count = 0; final int BUFSIZE = 1024; byte buf = new byteBUFSIZE; int len; while (len = fis.read(buf) != -1) for (int i = 0; i len; i+) if (bufi = A) count+; fis.close();27第27頁(yè),共83頁(yè)。 String filename = “test.txt”; FileReader fr = new FileReader(filename); BufferedReader br = new BufferedReader

28、(fr); int count = 0; while (br.readLine() != null) count+; br.close(); System.out.println(count);如何提高文件讀寫(xiě)效率-實(shí)例2如何提高文件讀寫(xiě)效率28第28頁(yè),共83頁(yè)。概述流的包裝(wrap) FileReader fr = new FileReader (filename); BufferedReader br = new BufferedReader (fr); BufferedReader br = new BufferedReader ( new FileReader(filename);

29、 PrintWriter out = new PrintWriter ( new BufferedWriter(new FileWriter(filename); public String readLine() in java.io.BufferedReader public void println(String x) in java.io.PrintWriter利用不同流的特點(diǎn)(方法)尋找合適的方法完成特定的需求對(duì)已有的流進(jìn)行再處理29第29頁(yè),共83頁(yè)。流的種類節(jié)點(diǎn)流直接對(duì)數(shù)據(jù)源進(jìn)行讀/寫(xiě)操作的流處理流對(duì)一個(gè)已有的流進(jìn)行某種操作的流流的包裝(wrap)30第30頁(yè),共83頁(yè)。節(jié)點(diǎn)流Fi

30、leInputStream, PipedInputStream, ByteArrayInputStream, StringBufferInputStreamFileOutputStream, PipedOutputStream, ByteArrayOutputStreamCharArrayReader, FileReader, PipedReader, StringReaderCharArrayWriter, FileWriter, PipedWriter, StringWriter流的包裝(wrap)31第31頁(yè),共83頁(yè)。處理流FilterOutputStream, DataOutputS

31、tream, BufferedOutputStream, PrintStreamFilterInputStream, LineNumberInputStream, DataInputStream, BufferedInputStream, PushbackInputStreamBufferedReader, LineNumberReader, InputStreamReader, FilterReader, PushBackReaderBufferedWriter, OutputStreamReader, FilterWriter, PrintWriter流的包裝(wrap)32第32頁(yè),共8

32、3頁(yè)。一個(gè)實(shí)例java.io.FileReaderpublic FileReader(File file) throws FileNotFoundExceptionpublic FileReader(String fileName) throws FileNotFoundExceptionjava.io.BufferedReaderpublic BufferedReader(Reader in) 8192 bytespublic BufferedReader(Reader in, int sz)流的包裝改變了流的行為流的包裝(wrap)33第33頁(yè),共83頁(yè)。流的包裝(wrap)-實(shí)例1流的包

33、裝(wrap)import java.io.*;public class Echo public static void main(String args) throws IOException BufferedReader in = new BufferedReader( new InputStreamReader(System.in); String s; while(s = in.readLine().length() != 0) System.out.println(s); public static final InputStream in (java.lang.System)pub

34、lic InputStreamReader(InputStream in)public BufferedReader(Reader in)程序如何結(jié)束(跳出while循環(huán))?34第34頁(yè),共83頁(yè)。概述java.io.DataInputStream類public DataInputStream(InputStream in)public final boolean readBoolean()public final byte readByte()public final char readChar()public final int readInt()java.io.DataOutputStr

35、eam類public DataOutputStream(OutputStream out)public final void writeBoolean(boolean v)public final void writeByte(int v)public final void writeChar(int v)public final void writeInt(int v)基本數(shù)據(jù)轉(zhuǎn)換流35第35頁(yè),共83頁(yè)。DataOutputStream out = new DataOutputStream(new FileOutputStream(invoice1.txt);double prices =

36、 19.99, 9.99;int units= 12, 8;String descs = Java T-shirt, Java Mug;for (int i = 0; i prices.length; i +) out.writeDouble(pricesi);out.writeChar(t);out.writeInt(unitsi);out.writeChar(t);out.writeChars(descsi);out.writeChar(n);out.close();基本數(shù)據(jù)轉(zhuǎn)換流-實(shí)例基本數(shù)據(jù)轉(zhuǎn)換流DataInputStream in = new DataInputStream( new

37、 FileInputStream(invoice1.txt);double total;try while (true) double price = in.readDouble(); in.readChar(); int unit = in.readInt(); in.readChar(); char chr; StringBuffer desc = new StringBuffer(20);while (chr = in.readChar()!= n) desc.append(chr);System.out.println(Youve ordered + unit + units of +

38、 desc + at $ + price); total = total + unit * price; catch (EOFException e) System.out.println(For a TOTAL of: $ + total);in.close();19.99 12 Java T-shirt9.99 8 Java Mug 36第36頁(yè),共83頁(yè)。概述java.io.File類: 文件和目錄的路徑名構(gòu)造方法public File(String pathname)public File(String parent, String child)public File(File par

39、ent, String child)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:windowsnotepad.exe”、“notepad.exe”目錄管理37第37頁(yè),共83頁(yè)。方法public boolean canWrite()/canRead()public boolean exists()public b

40、oolean delete() 刪除文件或目錄,若刪除目錄,要求該目錄必須為空public boolean createNewFile() 創(chuàng)建一個(gè)空文件,當(dāng)且僅當(dāng)該文件不存在public boolean isDirectory()/isFile()/isHidden()public long lastModified()/public boolean setLastModified(long time)public String list() 得到當(dāng)前目錄下的所有文件名和目錄名,若該File對(duì)象不是表示目錄,則返回nullpublic boolean mkdir()/mkdirs() 創(chuàng)建一

41、個(gè)目錄public boolean renameTo(File dest)public boolean setReadOnly()目錄管理38第38頁(yè),共83頁(yè)。目錄管理方法舉例目錄管理import java.io.File;public class DirList public static void main(String args) File path = new File(.);String list = path.list();for(int i = 0; i list.length; i+)System.out.println(listi);39第39頁(yè),共83頁(yè)。目錄管理方法舉例

42、目錄管理import java.io.File;import java.io.FilenameFilter;public class DirList public static void main(String args) File path = new File(.);String list = path.list(new DirFilter(args0); for (int i = 0; i list.length; i+) System.out.println(listi);class DirFilter implements FilenameFilter String key; Dir

43、Filter(String key) this.key = key; public boolean accept(File dir, String name) String f = new File(name).getName();return f.indexOf(key) != -1; java.util.FilenameFilter 接口 public boolean accept(File dir, String name); /是否指定的文件應(yīng)包含在文件列表中public String getName() in java.io.File/獲得文件或目錄名,僅僅是最后的部分40第40頁(yè),

44、共83頁(yè)。隨機(jī)訪問(wèn)文件(Random Access File)java.io.RandomAccessFile類讀寫(xiě)操作在同一個(gè)類中完成,須在構(gòu)造對(duì)象時(shí)指定參數(shù)通過(guò)移動(dòng)文件指針(file pointer)在文件的指定位置進(jìn)行讀寫(xiě)操作構(gòu)造方法public RandomAccessFile(String name, String mode) throws FileNotFoundExceptionpublic RandomAccessFile(File file, String mode) throws FileNotFoundExceptionmode: “r”, “rw”隨機(jī)訪問(wèn)文件41第41

45、頁(yè),共83頁(yè)。方法public void seek(long pos) public int read()public int read(byte b)public int read(byte b, int off, int len)public final boolean readBoolean()/readByte()/readChar()/readShort()/readInt()/readDouble()/readFloat()public final String readLine()public void write(int b)public void write(byte b)p

46、ublic void write(byte b, int off, int len)public final void writeBoolean()/writeByte()/writeChar()/writeShort()/writeInt()/writeDouble()/writeFloat()隨機(jī)訪問(wèn)文件42第42頁(yè),共83頁(yè)。隨機(jī)訪問(wèn)文件(Random Access File)-實(shí)例隨機(jī)訪問(wèn)文件RandomAccessFile rf = new RandomAccessFile(rtest.dat, rw);for(int i = 0; i 10; i+)rf.writeDouble(i

47、*1.414);rf.close();rf = new RandomAccessFile(rtest.dat, rw);rf.seek(5*8);rf.writeDouble(47.0001);rf.close();rf = new RandomAccessFile(rtest.dat, r);for(int i = 0; i 10; i+)System.out.println(Value + i + : + rf.readDouble();rf.close();運(yùn)行結(jié)果:Value 0: 0.0Value 1: 1.414Value 2: 2.828Value 3: 4.242Value 4

48、: 5.656Value 5: 47.0001Value 6: 8.484Value 7: 9.898Value 8: 11.312Value 9: 12.72599999999999943第43頁(yè),共83頁(yè)。文件的特性讀和寫(xiě)的權(quán)限文件長(zhǎng)度修改時(shí)間是否是目錄java.io.File類設(shè)定文件屬性查詢文件屬性文件屬性44第44頁(yè),共83頁(yè)。文件屬性獲取文件路徑import java.io.*;class AttrDemo1 public static void main(String args) throws IOException File testfile = new File(. + Fi

49、le.separatorChar + testfile1); testfile.createNewFile(); System.out.println(name = + testfile.getName(); System.out.println(path = + testfile.getPath(); System.out.println(absolute path = + testfile.getAbsolutePath(); System.out.println(canonical path = + testfile.getCanonicalPath(); 45第45頁(yè),共83頁(yè)。文件屬

50、性獲取文件修改時(shí)間import java.io.*; import java.util.*;public class AttrDemo2 public static void main(String args) throws IOException File testfile = new File(testfile2); testfile.delete(); testfile.createNewFile(); long modtime = testfile.lastModified(); System.out.println(last modification time #1 = + new

51、Date(modtime); testfile.setLastModified(0); modtime = testfile.lastModified(); System.out.println(last modification time #2 = + new Date(modtime);46第46頁(yè),共83頁(yè)。文件屬性獲取和設(shè)定文件長(zhǎng)度import java.io.*;public class AttrDemo3 public static void main(String args) throws IOException File testfile = new File(testfile

52、3); testfile.delete(); testfile.createNewFile(); System.out.println(length #1 = +testfile.length(); RandomAccessFile raf = new RandomAccessFile(testfile3, rw); raf.setLength(100); raf.close(); System.out.println(length #2 = + testfile.length(); 47第47頁(yè),共83頁(yè)。文件屬性設(shè)置讀寫(xiě)權(quán)限import java.io.*;public class Att

53、rDemo4 public static void main(String args) throws IOException File testfile = new File(testfile4); testfile.delete(); testfile.createNewFile(); if (testfile.canRead() System.out.println(file can be read #1); if (testfile.canWrite() System.out.println(file can be written #1); testfile.setReadOnly();

54、 if (testfile.canRead() System.out.println(file can be read #2); if (testfile.canWrite() System.out.println( file can be written #2); 48第48頁(yè),共83頁(yè)。第九章 結(jié)束 !49第49頁(yè),共83頁(yè)。概述URL應(yīng)用Socket應(yīng)用UDP數(shù)據(jù)報(bào)第十章 Java網(wǎng)絡(luò)編程50第50頁(yè),共83頁(yè)。概述The Java platform is highly regarded in part because of its suitability for writing pro

55、grams that use and interact with the resources on the Internet and the World Wide Web. 51第51頁(yè),共83頁(yè)。概述AppletApplet程序嵌在HTML文件中,通過(guò)網(wǎng)絡(luò)下載Applet程序代碼,在本地Java-enabled browser 中執(zhí)行HTTP通過(guò)URL類獲取服務(wù)器端的HTML文件Socket(套接字)實(shí)現(xiàn)Client/Server結(jié)構(gòu)的應(yīng)用JDBC (Java Database Connectivity)通過(guò)網(wǎng)絡(luò)訪問(wèn)關(guān)系型數(shù)據(jù)庫(kù)Oracle, MS SQL, SybaseServlet/JS

56、P (Java Server Page)WEB服務(wù)器端的動(dòng)態(tài)編程52第52頁(yè),共83頁(yè)。概述網(wǎng)絡(luò)基礎(chǔ)-TCP/IP協(xié)議簇網(wǎng)絡(luò)層(Network Layer)Internet Protocol (IP), IP地址, 32比特傳輸層(Transport Layer)傳輸控制協(xié)議(TCP: Transport Control Protocol)用戶數(shù)據(jù)報(bào)協(xié)議(UDP: User Datagram Protocol)端口(Port, 16比特, 065535)應(yīng)用層(Application Layer)HTTP, FTP, SMTP, POP3, Telnet, DNS TCP or UDPPort

57、應(yīng)用PortPortPortPort數(shù)據(jù)1應(yīng)用應(yīng)用應(yīng)用Port數(shù)據(jù)2主機(jī)53第53頁(yè),共83頁(yè)。概述Java語(yǔ)言中基本網(wǎng)絡(luò)類Package .URL.URLConnection.Socket.ServerSocket.DatagramPacket.DatagramSocket.MulticastSocket54第54頁(yè),共83頁(yè)。概述URL應(yīng)用Socket應(yīng)用UDP數(shù)據(jù)報(bào)第十章 Java網(wǎng)絡(luò)編程55第55頁(yè),共83頁(yè)。URL應(yīng)用什么是URL?統(tǒng)一資源定位符(Uniform Resource Locator)a reference (an address, a pointer) to a res

58、ource on the Internet.http:/協(xié)議標(biāo)識(shí)符資源名 (主機(jī)名, 端口號(hào), 文件名)http/index.html:/ftp/pub/:/56第56頁(yè),共83頁(yè)。URL應(yīng)用.URL類構(gòu)造方法public URL(String spec) throws MalformedURLExceptionpublic URL(String protocol, String host, String file) throws MalformedURLExceptionpublic URL(String protocol, String host, int port, String fil

59、e) throws MalformedURLException 實(shí)例方法public final InputStream openStream() throws IOExceptionOpens a connection to this URL and returns an InputStream for reading from that connectionpublic URLConnection openConnection() throws IOExceptionReturns a URLConnection object that represents a connection to

60、 the remote object referred to by the URL 57第57頁(yè),共83頁(yè)。URL應(yīng)用.URL類-示例“/”new URL();/academic/index.htmlnew URL(/academic/index.html);new URL(http, , /academic/index.html);new URL(http, , 80, “/academic/index.html);58第58頁(yè),共83頁(yè)。URL應(yīng)用實(shí)例import .*;import java.io.*;public class URLReader public static void m

溫馨提示

  • 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)論