Thuc hanh HPT

Embed Size (px)

Text of Thuc hanh HPT

I. MC TIU THC HNH

Gii thiu cho sinh vin cc kin thc cn bn v k thut RMI, y l k thut c s dng ph bin pht trin cc ng dng phn tn trong java. Pht trin cc ng dng phn tn khc nhau trn k thut RMI II. HC LIU Hc liu :- a USB - Phn mm (JDK1.5.0 hoc tr ln, Jcreator 4.0,NetBean 6.x )

- Bi tp thc hnh ca ging vin cung cp Dng c : - Projector, mn chiu - My tnh - Cc phn mm dy hc - Bng, phn - H thng my tnh c kt ni mng III. NI DUNG Gm 6 Lab vi cc ni dung sau: - LAB_01 : X l dng v File ( Bui 01) - LAB_02 : Lp trnh Socket v Multi Socket ( Bui 02) - LAB_03 : Lp trnh Threat v Multi Threat (Bui 03) - LAB_04 : Lp trnh UDP ( Bui 04) - LAB_05 : Lp trnh TCP ( Bui 05) - LAB_06 : Lp trnh Databsae( Bui 06) - LAB_07 : Lp trnh RMI (Bui 07,08) - LAB_08 : X l cc bi ton ng phn tn c bn bng k thut RMI (Bui 09) - Kim tra thc hnh VI.TI LIU THAM KHO Ti liu chnh [1]. Tp bi ging H phn tn , ca ging vin Nguyn Minh Nht [2]. Jie Wu, "Distributed Systems Design", Addison-Wesley, 2004 Ti liu tham kho [3]. S. Mullender ed., "Distributed Systems", 2nd ed., Addison-Wesley, 1993 [4]. G. Coulouris, J. Dollimore, T. Kinberg, "Distributed systems : Concept and Design" 1

[5]. Spiegel, A. (1998). Objects by value: Evaluating the trade-off. In Proceedings Int. Conf. on Parallel and Distributed Computing and Networks (PDCN), pages 542548, Brisbane, Australia. IASTED, ACTA Press. []. van Steen, M., Homburg, P., and Tanenbaum, A. (1999). Globe: A Wide-Area Distributed System. IEEE Concurrency, pages 70-78. [6]. Waldo, J., Wyant, G., Wollrath, A., and Kendall, S. (1997). A Note on Distributed Computing. In Vitek, J. and Tschudin, C., editors, Mobile Object Systems: Towards the Programmable Internet, volume 1222 of Lecture Notes in Computer Science, pages 49-64. Springer-Verlag. Ti liu Internet [5]. http://java.sun.com [6]. http://icp.org [7]. http://www.comp.dit.ie/coleary/teaching/dt2494ds/notes/11/index.html [8]. http://en.wikibooks.org/wiki/Distributed_Systems#Synchronization

2

TRNG I HC DUY TN KHOA CNG NGH THNG TIN B MN K THUT MNG

H PHN TN (Distributed System)

BI THC HNH Bi s : 01 S gi : 03gi GVHD : ThS.Nguyn Minh Nht

LAB 01 STREAMS & FILESI.MC TIU Cng c mt s kin thc lm c s cho thit k v lp trnh phn tn vi java, gm cc vn nh : - Dng v FileX l c, ghi trn dng v File

II. NI DUNG A. L THUYT Lung : Lung byte, lung k t a.Lung Byte : + Lp tru tng : InputStream v OutputStream + Cc phng thc h tr Lp lung byte BufferedInputStream BufferedOutputStream ByteArrayInputStream ByteArrayOutputStream DataInputStream DataOutputStream FileInputStream FileOutputStream FilterInputStream FilterOutputStream InputStream OutputStream PipedInputStream ngha Lung vo trn Bufered Lung ra trn Bufered Input stream c d liu t mt mng byte Output Stream ghi d liu t mt mng Lung nhp c nhng phng thc c nhng kiu d liu chun trong java Lung xut c nhng phng thc ghi nhng kiu d liu chun trong java Lung nhp cho php c d liu t file Lung xut cho php ghi d liu xung file Hin thc lp tru tng InputStream Hin thc lp tru tng OutputStream Lp tru tng, l lp cha ca tt c cc lp lung nhp kiu Byte Lp tru tng, l lp cha ca tt c cc lp xut nhp kiu Byte Lung nhp byte kiu ng (piped) thng phi c 3

gn vi mt lung xut kiu ng. Lung nhp byte kiu ng (piped) thng phi c gn vi mt lung nhp kiu ng to nn mt kt ni trao i d liu kiu ng. PrintStream Lung xut c cha phng thc print() v prinln() PushbackInputStream L mt lung nhp kiu Byte m h tr thao tc tr li (push back) v phc hi thao tc c mt byte (unread) RandomAccessFile H tr cc thao tc c, ghi i vi file truy cp ngu nhin. SequenceInputStream L mt lung nhp c to nn bng cch ni kt logic cc lung nhp khc. Nhng phng thc nh ngha trong lp InputStream v OutputStream PipedOutputStream Phng thcInputStream int available( ) void close( )

ngha- Tr v s lung bytes c th c c t lung nhp - ng lung nhp v gii phng ti nguyn h thng gn vi lung. Khng thnh cng s nm ra mt li IOException - nh du v tr hin ti trong lung nhp - Kim tra xem lung nhp c h tr phng thc mark() v reset() khng. c byte tip theo t lung nhp - c buffer.length bytes v lu vo trong vng nh buffer. Kt qu tr v s bytes tht s c c - c numBytes bytes bt u t a ch offset v lu vo trong vng nh buffer. Kt qu tr v s bytes tht s c c - Nhy con tr n v tr c xc nh bi vic gi hm mark() ln sau cng. - Nhy qua numBytes d liu t lung nhp - ng lung xut v gii phng ti nguyn h thng gn vi lung. Khng thnh cng s nm ra mt li IOException - p d liu t b m phi ghi ngay xung lung

void mark(int numBytes) boolean markSupported( ) int read( ) int read(byte buffer[ ])

int read(byte buffer[ ], int offset, int numBytes) void reset( ) long skip(long numBytes) Output Stream void close( )

4

void flush( ) void write(int b) void write(byte buffer[ ]) void write(byte buffer[ ], int offset, int numBytes)

(nu c) - Ghi byte d liu ch nh xung lung - Ghi buffer.length bytes d liu t mng ch nh xung lung - Ghi numBytes bytes d liu t v tr offset ca mng ch nh buffer xung lung

b.Lung k t (Character Streams) + Lp tru tng : Reader v Writer + Cc phng thc h tr Lp lung k tBufferedReader BufferedWriter CharArrayReader CharArrayWriter FileReader FileWriter FilterReader FilterWriter InputStreamReader LineNumberReader OutputStreamWriter PipedReader PipedWriter PrintWriter PushbackReader Reader StringReader StringWriter Writer

ngha

Lung nhp k t c d liu vo mt vng m Lung xut k t ghi d liu ti mt vng m Lung nhp c d liu t mt mng k t Lung xut ghi d liu ti mt mng k t Lung nhp k t c d liu t file Lung xut k t ghi d liu n file Lp c d liu trung gian(lp tru tng) Lp xut trung gian tru tng Lung nhp chuyn bytes thnh cc k t (*) Lung nhp m dng Lung xut chuyn nhng k t thnh cc bytes(*) Lung c d liu bng c ch ng ng Lung ghi d liu bng c ch ng ng Lung ghi vn bn ra thit b xut (cha phng thc print() v println() ) Lung nhp cho php c v khi phc li d liu Lp nhp d liu tru tng Lung nhp c d liu t chui Lung xut ghi d liu ra chui Lp ghi d liu tru tng

B.BI TP 1.1. Hy c mt mng byte t System.in, sau khi nhp t bn phm. 1.2. S dng phng thc System.out.write() xut k t X ra Console 1.3. Hy c cc k t t console s dng BufferedReader, cho n khi gp du chm th ngng li. 1.4. Hy c cc chui t console s dng BufferedReader, nhng n ch stop th ngng li. 1.5. Hy to mt file text s dng BufferedReader

5

1.6. Hy xut ra Console dng lung k t c kiu thay i 1.7. Hin th ni dung ca mt file tn test.txt lu ti D:\test.txt 1.8. Copy ni dung mt file text n mt file text khc 1.9. Dng DataOutputStream v DataInputStream ghi v c nhng kiu d liu khc nhau trn file. 1.10. Ghi 6 s kiu double xung file, ri c ln theo th t ngu nhin. 1.11. 1.12.c nhng dng vn bn nhp t bn phm v ghi chng xung file tn l test.txt. Vic c v ghi kt thc khi ngi dng nhp vo chui stop. HNG DN 1.1. S dng phng thc read c ni dung t system.inimport java.io.*; class ReadBytes { public static void main(String args[]) throws IOException { byte data[] = new byte[100]; System.out.print("Ban hay nhap mot so ky tu "); System.in.read(data); // Doc chuoi byte da nhap vao data System.out.print(" Cac ky tu cua ban da nhap: "); for(int i=0; i < data.length; i++) System.out.print((char) data[i]); } }

1.3. Nhp Console dng lung k t Thng th vic nhp d liu t Console dng lung k t th thun li hn dng lung byte. Lp tt nht c d liu nhp t Console l lp BufferedReader. Tuy nhin chng ta khng th xy dng mt lp BufferedReader trc tip t System.in. Thay vo chng ta phi chuyn n thnh mt lung k t. lm iu ny chng ta dng InputStreamReader chuyn bytes thnh k t. c c mt i tng InputStreamReader gn vi System.in ta dng constructor ca InputStreamReader :InputStreamReader(InputStream inputStream)

Tip theo dng i tng InputStreamReader to ra to ra mt BufferedReader dng constructor BufferedReader. BufferedReader(Reader inputReader) V d: To mt BufferedReader gn vi KeyboardBufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Sau khi thc hin cu lnh trn, br l mt lung k t gn vi Console thng qua System.in. 6

1.4

import java.io.*; class ReadChars { public static void main(String args[]) throws IOException { char c; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Nhap chuoi ky tu, gioi han dau cham."); // Doc chuoi ky tu do { c = (char)br.read(); System.out.println(c); } while(c != '.'); } } hng dn : BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); import java.io.*; class ReadChars { public static void main(String args[]) throws IOException { char c; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Nhap chuoi ky tu, gioi han stop "); // Doc chuoi ky tu do Readline : c theo hng ngang { c = br.readline(); System.out.println(c); } while(!c.readline(stop)); } }

1.5

:

hng dn : ging 1.6,1.7

1.6. Trong ngn ng java, bn cnh vic dng System.out xut d liu ra Console (thng dng debug chng trnh), chng ta c th dng lung PrintWriter i vi cc chng trnh chuyn nghip. PrintWriter l mt trong nhng lp lung k t.

7

Vic dng cc lp lung k t xut d liu ra Console thng c a chung hn. xut d liu ra Console dng PrintWriter cn thit phi ch nh System.out cho lung xut. V d: To i tng PrintWriter xut d liu ra Console PrintWriter pw = new PrintWriter(System.out, true); 1.7. c d liu t file M mt file c d liu FileInputStream(String fileName) FileNotFoundException Nu file khng tn ti: th nm ra FileNotFoundException throws

c d liu: dng phng thc read() int read( ) throws IOException: c tng byte t file v tr v gi tr ca byte c c. Tr v -1 khi ht file, v nm ra IOException khi c li c. ng file: dng phng thc close() void close( ) throws IOException: sau khi lm vic xong cn ng file gii phng ti nguyn h thng cp pht cho file. 1.8. Ghi d liu xung file M mt file ghi d liu FileOutputStream(String fileName) throws FileNotFoundException Nu file khng to c: th nm ra FileNotFoundException Ghi d liu xung: dng phng thc write() void write(int byteval) throws IO