e.printStackTrace(System.out);
}
}
}
<b>4.writeaclientprogramlikethis</b>
//HelloClient.java
importHelloApp.*;
importorg.omg.CosNaming.*;
importorg.omg.CORBA.*;
publicclassHelloClient
{
publicstaticvoidmain(Stringargs[])
{
try{
//createandinitializetheORB
ORBorb=ORB.init(args,null);
//gettherootnamingcontext
org.omg.CORBA.ObjectobjRef=
orb.resolve_initial_references("NameService");
NamingContextncRef=NamingContextHelpe
r.narrow(objRef);
//test
System.out.println("OK..");
//resolvetheObjectReferenceinNaming
NameComponentnc=newNameComponent(
"Hello","");
NameComponentpath[]={nc};
HellohelloRef=HelloHelper.narrow(ncRef.re
solve(path));
//calltheHelloserverobjectandprintresults
//Stringoldhello=helloRef.lastMessage();
//System.out.println(oldhello);
StringHello=helloRef.sayHello();
System.out.println(Hello);
}catch(Exceptione){
System.out.println("ERROR:"+e);
e.printStackTrace(System.out);
}
}
}
<b>5.compliethesefiles</b>
javac*.javaHelloApp/*.java
<b>6.runtheapplication</b>
a.firstyou’vetoruntheNameServicepriortothe
otherslikethis
c:\>tnameserv
b.runserver
c:\>javaHelloServer
c.runclient
c:\>javaHelloClient
利用 RamdonAccessFile 来实现文件的追加
RamdonAccessFile是个很好用的类,功能十分强
大,可以利用它的
length()和seek()方法来轻松实现文件的追加,相信
我下面这个例子是
很容易看懂的,先写入十行,用 length()读出长度
(以 byte 为单位),
在用 seek()移动到文件末尾,继续添加,最后显示
记录。
importjava.io.*;
publicclassIOStreamDemo{
publicstaticvoidmain(String[]args){
try{
RandomAccessFilerf1=newRandomAccess
File("d:\\jeru.txt","rw");
for(inti=0;i<10;i++) {
rf1.writeBytes("xixi,thisisline"+i+"\n");
}
rf1.close();
inti=0;
Stringrecord=newString();
RandomAccessFilerf2=newRandomAccess
File("d:\\jeru.txt","rw");
rf2.seek(rf2.length());
rf2.writeBytes("lala,appendline"+"\n");