![《 JAVA實(shí)現(xiàn)遠(yuǎn)程文件讀取!》_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/2/12934173-0b51-479c-b192-6a681d87d227/12934173-0b51-479c-b192-6a681d87d2271.gif)
![《 JAVA實(shí)現(xiàn)遠(yuǎn)程文件讀取!》_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/2/12934173-0b51-479c-b192-6a681d87d227/12934173-0b51-479c-b192-6a681d87d2272.gif)
![《 JAVA實(shí)現(xiàn)遠(yuǎn)程文件讀取!》_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/2/12934173-0b51-479c-b192-6a681d87d227/12934173-0b51-479c-b192-6a681d87d2273.gif)
![《 JAVA實(shí)現(xiàn)遠(yuǎn)程文件讀取!》_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/2/12934173-0b51-479c-b192-6a681d87d227/12934173-0b51-479c-b192-6a681d87d2274.gif)
![《 JAVA實(shí)現(xiàn)遠(yuǎn)程文件讀取!》_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/2/12934173-0b51-479c-b192-6a681d87d227/12934173-0b51-479c-b192-6a681d87d2275.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 = import java.io.*; import .*; public class RemoteFileClient protected String hostIp; protected int hostPort; protected BufferedReader socketReader; protected PrintWriter socketWriter; public static void main(String args) RemoteFileClient remoteFileClient = new RemoteFileClient(127.0.0.1, 3000); re
2、moteFileClient.setUpConnection(); String fileContents =remoteFileClient.getFile(E:RemoteFile.txt); remoteFileClient.tearDownConnection(); System.out.println(fileContents); /= public RemoteFileClient(String aHostIp, int aHostPort) hostIp = aHostIp; hostPort = aHostPort; /= public void setUpConnection
3、() try Socket client = new Socket(hostIp, hostPort); socketReader = new BufferedReader( new InputStreamReader(client.getInputStream(); socketWriter = new PrintWriter(client.getOutputStream(); catch (UnknownHostException e) System.out.println(Error setting up socket connection: unknown host at + host
4、Ip + : + hostPort); catch (IOException e) System.out.println(Error setting up socket connection: + e); /= public String getFile(String fileNameToGet) StringBuffer fileLines = new StringBuffer(); try socketWriter.println(fileNameToGet); socketWriter.flush(); String line = null; while (line = socketRe
5、ader.readLine() != null) fileLines.append(line + n); catch (IOException e) System.out.println(Error reading from file: + fileNameToGet); return fileLines.toString(); /= public void tearDownConnection() try socketWriter.close(); socketReader.close(); catch (IOException e) System.out.println(Error tea
6、ring down socket connection: + e); /= = 服務(wù)器端 = import java.io.*; import .*; public class RemoteFileServer protected int listenPort = 3000; public static void main(String args) RemoteFileServer server = new RemoteFileServer(); server.acceptConnections(); public void acceptConnections() try ServerSock
7、et server = new ServerSocket(listenPort); Socket incomingConnection = null; while (true) incomingConnection = server.accept(); handleConnection(incomingConnection); catch (BindException e) System.out.println(Unable to bind to port + listenPort); catch (IOException e) System.out.println(Unable to ins
8、tantiate a ServerSocket on port: + listenPort); public void handleConnection(Socket incomingConnection) try OutputStream outputToSocket = incomingConnection.getOutputStream(); InputStream inputFromSocket = incomingConnection.getInputStream(); BufferedReader streamReader = new BufferedReader(new Inpu
9、tStreamReader(inputFromSocket); FileReader fileReader = new FileReader(new File(streamReader.readLine(); BufferedReader bufferedFileReader = new BufferedReader(fileReader); PrintWriter streamWriter = new PrintWriter(incomingConnection.getOutputStream(); String line = null; while (line = bufferedFile
10、Reader.readLine() != null) streamWriter.println(line); fileReader.close(); streamWriter.close(); streamReader.close(); catch (Exception e) System.out.println(Error handling a client: + e); = 輸出結(jié)果: -看!我讀取了E:RemoteFile.txt啊! = Wrapping up the server Before we move on to another, more practical example
11、, lets review the steps to create and use a ServerSocket: 1. Instantiate a ServerSocket with a port on which you want it to listen for incoming client connections (throws an Exception if theres a problem). 2. Call accept() on the ServerSocket to block while waiting for connection. 3. Get the streams
12、 on that underlying Socket for reading and writing. 4. Wrap the streams as necessary to simplify your life. 5. Read from and write to the Socket. 6. Close your open streams (and remember, never close your Reader before your Writer). You can find the complete code listing for RemoteFileServer at Code
13、 listing for RemoteFileServer on page 33. = Wrapping up the server Before we move on to another, more practical example, lets review the steps to create and use a ServerSocket: 1. Instantiate a ServerSocket with a port on which you want it to listen for incoming client connections (throws an Exception if theres a problem). 2. Call accept() on the ServerSocket to block while waiting for connection. 3. Get the streams on that underlying So
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年羅紋鋼扁鋼元鋼冷軋項(xiàng)目投資可行性研究分析報(bào)告
- 水利樞紐(水庫)工程建設(shè)項(xiàng)目社會(huì)穩(wěn)定風(fēng)險(xiǎn)評(píng)估報(bào)告-
- 知識(shí)產(chǎn)權(quán)申請(qǐng)技巧從零到一的成功之路
- 鐵路職工對(duì)調(diào)申請(qǐng)書
- 內(nèi)勤轉(zhuǎn)正申請(qǐng)書
- 無汞可充電堿錳電池項(xiàng)目安全評(píng)估報(bào)告
- 電子病歷系統(tǒng)在提升患者體驗(yàn)中的應(yīng)用
- 中國衣領(lǐng)凈洗滌劑市場供需現(xiàn)狀及投資戰(zhàn)略研究報(bào)告
- 2024河南石油鉆采專用設(shè)備制造市場前景及投資研究報(bào)告
- 生物醫(yī)藥產(chǎn)業(yè)的市場營銷策略與實(shí)踐
- 2022人臉識(shí)別安全白皮書
- 【建模教程】-地質(zhì)統(tǒng)計(jì)學(xué)礦體建模簡明教材
- 無人機(jī)應(yīng)用案例-石油領(lǐng)域油氣管線巡查
- DB23T 2656-2020樺樹液采集技術(shù)規(guī)程
- 重源煤礦 礦業(yè)權(quán)價(jià)款計(jì)算書
- PSM工藝安全管理
- GB/T 21872-2008鑄造自硬呋喃樹脂用磺酸固化劑
- 上海市中小學(xué)生語文學(xué)業(yè)質(zhì)量綠色指標(biāo)測試
- GA/T 501-2020銀行保管箱
- 《育兒百科》松田道雄(最新版)
- 軸對(duì)稱圖形導(dǎo)學(xué)案
評(píng)論
0/150
提交評(píng)論