Implementasi b eberapa cla ss System.IO

13.2.2 Implementasi b eberapa cla ss System.IO

Implem entasi beberapa class System.IO akan ditampilkan mengguna kan console dan window s form. Contoh menggunakan conso le adalah seperti ini :

s t r i ng fi l eName = Pat h. Get TempFi l eName( ) ;

Fi l eI nf o f i l eI nf o = new Fi l eI nf o( f i l eName) ; Cons ol e. Wr i t eLi ne( " Fi l e ' { 0} ' c r eat ed of s i z e { 1} by t e s",

f i l eName, f i l eI nf o. Lengt h) ;

/ / Append s ome t ex t t o t he f i l e. St r e amWr i t er s = f i l eI nf o. AppendTex t ( ) ; s . Wr i t eLi ne( " The t ex t i n t he f i l e" ) ; s . Cl os e( ) ;

f i l eI nf o. Ref r es h( ) ; Cons ol e. Wr i t eLi ne( " Fi l e ' { 0} ' now has s i z e { 1} by t es " ,

f i l eName, f i l eI nf o. Lengt h) ;

/ / Read t he t ex t f i l e St r eamReader r = f i l eI nf o. OpenTex t ( ) ; s t r i ng t ex t Li ne; whi l e ( ( t ex t Li ne = r . ReadLi ne( ) ) ! = nul l ) {

Cons ol e. Wr i t eLi ne( t ex t Li ne) ; } r . Cl os e( ) ;

Jika menggunakan windows form langkah-langkahnya adalah : Buat satu windows application project baru. drag 1 button,1 textbox dan 1 richtextbox lalu drop di form. ketikan di dalam method button seperti ini :

pr i v at e v oi d but t on1_Cl i c k ( obj ec t s ender , Sy s t em. Ev ent Ar gs e)

s t r i ng f i l eName = " C: \ \ COBA. TXT" ;

Fi l eI nf o f i l eI nf o = new Fi l eI nf o( f i l eName) ;

// menambahk an t ex t ke file St r eamWr i t er s = f i l eI nf o. AppendTex t ( ) ; s . Wr i t eLi ne( " The t ex t in t he f i l e" ) ;

s . Cl os e( ) ;

f i l eI nf o. Ref r es h( ) ;

s t r i ng t ex t Li ne; // membac a file t ex t St r eamReader r = f i l eI nf o. OpenTex t ( ) ;

whi l e ( ( t ex t Li ne = r . ReadLi ne( ) ) ! = nul l ) {

t ex t Li ne2 += t ex t Li ne + " \ n" ;

r i c hTex t Box 1. Tex t = t ex t Li ne2;

r . Cl os e( ) ;

tambahkan “string textLine2” seperti ini :

publ i c c l as s For m1 : Sy s t em. Wi ndows . For ms . For m {

s t r i ng t ex t Li ne2; ... ...

klik run, lalu tekan tombol.