📄 15-通过java套接字传递对象(2).htm
字号:
FONT-WEIGHT: bold; COLOR: #ab6503}.g4 { COLOR: #ff0000}.img { BORDER-RIGHT: #800000 1px solid; BORDER-TOP: #800000 1px solid; BORDER-LEFT: #800000 1px solid; BORDER-BOTTOM: #800000 1px solid}.img1 { BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid}.f14 { FONT-SIZE: 14px; LINE-HEIGHT: 26px}.f20 { FONT-WEIGHT: bold; FONT-SIZE: 20px}.top_red { COLOR: #d70709; TEXT-DECORATION: none}.center01 { COLOR: #000000; LINE-HEIGHT: 20px}.center01 TD { COLOR: #000000; LINE-HEIGHT: 20px}.center01 A:link { COLOR: #000000; TEXT-DECORATION: none}.center01 A:visited { COLOR: #000000; TEXT-DECORATION: none}.center01 A:hover { COLOR: #2b7128; TEXT-DECORATION: underline}.center02 { COLOR: #194e00; LINE-HEIGHT: 20px}.center02 TD { COLOR: #194e00; LINE-HEIGHT: 20px}.center02 A:link { COLOR: #194e00; TEXT-DECORATION: none}.center02 A:visited { COLOR: #194e00; TEXT-DECORATION: none}.center02 A:hover { COLOR: #194e00; TEXT-DECORATION: underline}--></style><TABLE width=760 border=0 align=center cellPadding=0 cellSpacing=0 bgcolor="#F3F3F3" valign="top"> <tr class="center01"> <td width="488" style="padding-left:10px;padding-top:10px"> <a href="index.php">连载</a> > <a href="slist.php?class1=6">程序设计</a> > <a href="slist.php?class2=7">Java</a> > <a href="serialize.php?id=387">Java网络编程专辑</a> </td> <td width="280" style="padding-left:10px;padding-top:10px"> <div align="center"> <font color=#0000FF><a href='chapter.php?id=387&volume=3&chapter=14'>上一页</a></font> <font color=#0000FF><a href="serialize.php?id=387">回书目</a></font> <font color=#0000FF><a href='chapter.php?id=387&volume=4&chapter=1'>下一页</a></font> </div> </td> </tr> <tr class="center01"> <td colspan="2" align="center" style="padding-left:10px;padding-top:10px"> </td> </tr> <TBODY> <TR> <TD colspan="2" align=middle> <BR> <div style="FONT-SIZE: 18pt; COLOR: #990000; FONT-FAMILY: 楷体_GB2312" align=center><B>Socket套接字</b></div><br><div style="COLOR: #990000; font-family: ; font-size: 18px;宋体;" align=center>通过Java套接字传递对象(2)</div> <DIV style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; FONT-SIZE: 10.5pt; COLOR: black; LINE-HEIGHT: 180%" align=left> <br> <PRE><SPAN class=f14> <STRONG>序列化用户自己的类</STRONG> </PRE>
<P></P>
<P> 现在,让我们看看如何序列化用户自己写的类。在这个例子中,我们将建立一个用户类UserInfo,见例程3。为了让它可序列化,UserInfo类实现了Serializable接口。</P>
<P> 例程 3: UserInfo.java </P>
<TABLE cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>
<P>import java.io.*;<BR>import java.util.*;</P>
<P>public class UserInfo implements Serializable {<BR>String name = null;</P>
<P>public UserInfo(String name) {<BR>this.name = name;<BR>}</P>
<P>public void printInfo() {<BR>System.out.println("The name is: "+name);<BR>}<BR>}</P></TD></TR></TBODY></TABLE>
<P> 下一步就是建立一个能创建UserInfo类实例的类,然后将对象写入输出流中,如例程4。本例中的输出流是一个名为"name.out"的文件。要注意的是,例程4 中的writeObject方法可以被调用任意多次,将任意多个对象写入输出流。</P>
<P> 例程 4: SaveInfo.java </P>
<TABLE cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>
<P>import java.io.*;<BR>import java.util.Date;</P>
<P>public class SaveInfo {</P>
<P>public static void main(String argv[]) throws Exception {<BR>FileOutputStream fos = new FileOutputStream("name.out");<BR>ObjectOutputStream oos = new ObjectOutputStream(fos);<BR>// create two objects<BR>UserInfo user1 = new UserInfo("Java Duke");<BR>UserInfo user2 = new UserInfo("Java Blue");<BR>// write the objects to the output stream<BR>oos.writeObject(user1);<BR>oos.writeObject(user2);<BR>oos.flush();<BR>oos.close();<BR>fos.close();<BR>}<BR>}</P></TD></TR></TBODY></TABLE>
<P> 最后,我们写一个将已经保存的对象读入的类,并且调用一个如例程5所示的方法。和writeObject 一样,readObject方法能被调用任意多次,从输入流中读入任意多个对象。</P>
<P> 例程 5: ReadInfo.java </P>
<TABLE cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>
<P>import java.io.*;<BR>import java.util.Date;</P>
<P>public class ReadInfo {</P>
<P>public static void main(String argv[]) throws Exception {<BR>FileInputStream fis = new FileInputStream("name.out");<BR>ObjectInputStream ois = new ObjectInputStream(fis);<BR>// read the objects from the input stream (the file name.out)<BR>UserInfo user1 = (UserInfo) ois.readObject();<BR>UserInfo user2 = (UserInfo) ois.readObject();<BR>// invoke a method on the constructed object<BR>user1.printInfo();<BR>user2.printInfo();<BR>ois.close();<BR>fis.close();<BR>}<BR>}</P></TD></TR></TBODY></TABLE>
<P> 要测试这个例子,请编译如下源文件:UserInfo.java, SaveInfo.java, 和 ReadInfo.java。运行 SaveInfo,然后运行ReadInfo,将看到类似下面的输出结果: </P>
<P> The name is: Java Duke<BR> The name is: Java Blue</P></SPAN><br><br> </DIV> </TD> </TR> <TR> <TD class=center01> <div align="center">来源:天极网 作者:<div> </TD> <TD width="280" class=center01> <div align="center"> <font color=#0000FF><a href='chapter.php?id=387&volume=3&chapter=14'>上一页</a></font> <font color=#0000FF><a href="serialize.php?id=387">回书目</a></font> <font color=#0000FF><a href='chapter.php?id=387&volume=4&chapter=1'>下一页</a></font> </div> </TD> </TR> </TBODY></TABLE><table><tr><td width="760"> <table width="760" height="10" border="0" cellpadding="0" cellspacing="0"> <tr> <td></td> </tr> </table> <!------------ 评论 ----------------> <table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td><iframe width="100%" id=vs frameborder=0 scrolling=no src="comment_list1.php?id=387"></iframe> </td> </tr> </table><br><script language=javascript>function CheckNetwordForm(theForm){ if("" == theForm.content.value) { alert("写两句吧~~"); theForm.content.focus(); return false; } var index; for(index=0;index<theForm.content.value.length;index++) { if(" " != theForm.content.value.charAt(index)) break; } if(index == theForm.content.value.length) { alert("写两句吧~~"); theForm.content.focus(); return false; } if (theForm.content.value.length>100){ alert("评论字数不能超过100哦"); theForm.content.focus(); return false; } return true;}</script> <!------------------ 评论 ---------------> <form name=netword method=post action="insertnetword.php" onsubmit="javascript: return CheckNetwordForm(this);"> <table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="25" class="text6"> 给此书打分:<a name="1"></a> <select name="score"> <option value=5 selected>非常好</option> <option value=4>还凑合</option> <option value=3>一般吧</option> <option value=2>不太行</option> <option value=1>太差了</option> </select> 用户名: <input name="id" value=387 type="hidden" id="id"> <input name="backurl" value=/book/chapter.php?id=387&volume=3&chapter=15 type="hidden"> <input name="username" type="text" id="username" size="20" maxlength="20"> <font color="#666666">*评论字数请控制在一百字以内</font> </td> </tr> </table> <br> <textarea name="content" cols="80" rows="4" wrap="OFF" id="description"></textarea> <input type="submit" name="Submit" value="提交"> </form> </td> </tr></table></td></tr></table> <TABLE cellSpacing=0 cellPadding=0 width=760 border=0> <TBODY> <TR> <TD width="1003" height=9 background=images/t_bj01.gif><IMG height=1 src="images/ccc.gif" width=1></TD> </TR> </TBODY></TABLE><TABLE cellSpacing=0 cellPadding=0 width=760 bgColor=#ffffff border=0> <TBODY> <TR> <TD><HR width=760 noShade SIZE=1> </TD> </TR> <TR> <TD align=middle><A class=black href="http://www.chinaren.com/" target=_blank>ChinaRen</A> - <A class=black href="http://big5.www.sohu.com/" target=_blank>繁体版</A> - <A class=black href="http://hr.sohu.com/hrm.html" target=_blank>搜狐招聘</A> - <A class=black href="http://add.sohu.com/" target=_blank>网站登录</A> - <A class=black href="http://help.sohu.com/" target=_blank>帮助中心</A> - <A class=black href="http://book.news.sohu.com/onClick=this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.sohu.com');return" target=_blank false;>设置首页</A> - <A class=black href="http://adinfo.sohu.com/" target=_blank>广告服务</A> - <A class=black href="http://www.sohu.com/about/lianxi.htm" target=_blank>联系方式</A> - <A class=black href="http://www.sohu.com/about/privacy.html" target=_blank>保护隐私权</A> - <A class=black href="http://www.sohu.com/about/" target=_blank>About SOHU</A> - <A class=black href="http://www.sohu.com/about/" target=_blank>公司介绍</A><BR> <SPAN class=eng>Copyright © 2004 Sohu.com Inc. All rights reserved. 搜狐公司 版权所有</SPAN> </TD> </TR> </TBODY></TABLE></center></body></html><script language="JavaScript" src="http://nielsen.js.sohu.com/nnselect.js"></script><noscript><img src='http://ping.nnselect.com/ping.gif?c=119' height='1' width='1'></noscript>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -