Java序列化的作用是什么
1、序列化是干什么的?
簡單說就是為了保存在內存中的各種對象的狀態,并且可以把保存的對象狀態再讀出來。雖然你可以用你自己的各種各樣的方法來保存ObjectStates,但是Java給你提供一種應該比你自己好的保存對象狀態的機制,那就是序列化。
2、什么情況下需要序列化
a)當你想把的內存中的對象保存到一個文件中或者數據庫中時候;
b)當你想用套接字在網絡上傳送對象的時候;
c)當你想通過RMI傳輸對象的時候;
3、當對一個對象實現序列化時,究竟發生了什么?
在沒有序列化前,每個保存在堆(Heap)中的對象都有相應的狀態(state),即實例變量(instanceariable)比如:
FoomyFoo=newFoo();
myFoo.setWidth(37);
myFoo.setHeight(70);
當通過下面的代碼序列化之后,MyFoo對象中的width和Height實例變量的值(37,70)都被保存到
foo.ser
文件中,這樣以后又可以把它從文件中讀出來,重新在堆中創建原來的對象。當然保存時候不僅僅是保存對象的實例變量的值,JVM還要保存一些小量信息,比如類的類型等以便恢復原來的對象。FileOutputStreamfs=newFileOutputStream("foo.ser");
ObjectOutputStreamos=newObjectOutputStream(fs);
os.writeObject(myFoo);
4、實現序列化(保存到一個文件)的步驟
a)MakeaFileOutputStream
java代碼
FileOutputStreamfs=newFileOutputStream("foo.ser");
b)MakeaObjectOutputStream
java代碼
ObjectOutputStreamos=newObjectOutputStream(fs);
c)writetheobject
java代碼
os.writeObject(myObject1);
os.writeObject(myObject2);
os.writeObject(myObject3);
d)closetheObjectOutputStream
java代碼
os.close();
5、舉例說明
java代碼
import.*;
publicclassBoximplementsSerializable
{
privateintwidth;
privateintheight;
publicvoidsetWidth(intwidth){
this.width
=width;}
publicvoidsetHeight(intheight){
this.height
=height;}
publicstaticvoidmain(String[]args){
BoxmyBox=newBox();
myBox.setWidth(50);
myBox.setHeight(30);
try{
FileOutputStreamfs=newFileOutputStream("foo.ser");
ObjectOutputStreamos=newObjectOutputStream(fs);
os.writeObject(myBox);
os.close();
}catch(Exceptionex){
ex.printStackTrace();
}
}
}
6、相關注意事項
a)當一個父類實現序列化,子類自動實現序列化,不需要顯式實現;
b)當一個對象的實例變量引用其他對象,序列化該對象時也把引用對象進行序列化;
c)并非所有的對象都可以序列化,,至于為什么不可以,有很多原因了,比如:
1.安全方面的原因,比如一個對象擁有private,public等field,對于一個要傳輸的對象,比如寫到文件,或者進行rmi傳輸等等,在序列化進行傳輸的過程中,這個對象的private等域是不受保護的。
2.資源分配方面的原因,比如socket,thread類,如果可以序列化,進行傳輸或者保存,也無法對他們進行重新的資源分配,而且,也是沒有必要這樣實現。
printexception怎么解決
printexception解決方法如下
1.確認錯誤代碼的位置,并進行調試。
2.檢查你的代碼是否正確地處理了可能的異常和錯誤情況。
3.確認是否針對特定的異?;蝈e誤情況定義了"PrintException"函數,并檢查該函數是否正確引用或調用。
4.嘗試插入一些斷點或調試語句,以了解程序在哪些點上出現了問題。
總之,由于"PrintException"是一個自定義函數名,可能會因為不同的代碼和上下文而導致不同的錯誤情況,所以具體解決方法也需要具體問題具體分析,需要對程序進行深入的調試和分析。
freemarker導出word文檔,怎么寫
將要導出的Word另存為xml格式的文件,打開xml在其中添加freemarker標簽,然后另存為outChairDoc.ftl文件.
第一步要加入Freemarker.jar包。
Servlet代碼如下:在outChairDoc.ftl放在包cn.skyclass.jiaowu.freemarker.template下
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
configuration=newConfiguration();
configuration.setDefaultEncoding("utf-8");
try{
createDoc(response);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicvoidcreateDoc(HttpServletResponseresponse)throwsException{
//要填入模本的數據文件
MapdataMap=newHashMap();
dataMap.put("docTitle","fdfdfdfdfdfdf用戶信息");
Listlist=newArrayList();
ChairInfochairInfo=newChairInfo();
chairInfo.setChairTitle("dfdfd");
chairInfo.setTeacherName("tea");
chairInfo.setStartTime(newDate());
chairInfo.setPlace("dfdfd");
list.add(chairInfo);
dataMap.put("chairInfoList",list);
//設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載,
//這里我們的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/cn/skyclass/jiaowu/freemarker/template");
Templatet=null;
try{
//test.ftl為要裝載的模板
t=configuration.getTemplate("outChairDoc.ftl");
t.setEncoding("utf-8");
}catch(IOExceptione){
e.printStackTrace();
}
//輸出文檔路徑及名稱
StringfileName="講座列表.doc";
response.setContentType("application/msword");
response.addHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(fileName,"UTF-8"));//可防止導出的文件亂碼
response.setCharacterEncoding("utf-8");
PrintWriterout=response.getWriter();
t.process(dataMap,out);
out.close();
}
基于JSP實現一個簡單計算器的方法
<!--這是一個計算器-->
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<h1>計算器</h1>
<hr>
<scriptlanguage="javascript">
functioncheckNum(){
// alert("1");alert("0");
if(document.form1.num1.value==""){//注意這里只能使用==不能使用equals("")以為js中沒有equals函數 alert("num為空");
returnfalse;
}
//判斷輸入的的是不是數字
if(Math.round(document.form1.num1.value)!=document.form1.num1.value){
alert("輸入的不是num1數字類型。請核實");
returnfalse;
}
if(Math.round(document.form1.num2.value)!=document.form1.num2.value){
alert("輸入的不是num2數字類型。請核實");
returnfalse;
}
if(document.form1.operator.value=="/"&&document.form1.num2.value==0){
alert("除數不能為0");
returnfalse;
}
}
</script>
<body>
<!--顯示結果-->
<!--
<%
//接受第一個運算數
StringstrNum1=request.getParameter("num1");
//接受第二個云算數
StringstrNum2=request.getParameter("num2");
//System.out.println("strNum2="+strNum2);
//接受運算符
Stringoperator=request.getParameter("operator");
//計算結果
intnum11=0,num22=0,result=0;
out.println("12345");
if(strNum1!=null&&strNum2!=null&&operator!=null){
out.println("不等于空=============");
// returnfalse;
try{
num11=Integer.parseInt(strNum1);
num22=Integer.parseInt(strNum2);
if(operator.equals("+")){
result=num11+num22;
}elseif(operator.equals("-")){
result=num11-num22;
}elseif(operator.equals("*")){
result=num11*num22;
}elseif(operator.equals("/")){
result=num11/num22;
}
}catch(Exceptione){
e.printStackTrace();
out.println("12345678");
}
// out.println(strNum1+operator+strNum2+"="+result);
}
%>
-->
<formname="form1"action="myCal.jsp">
請輸入第一個數:<inputtype="text"name="num1"value="<%=strNum1%>"><br>
<selectname="operator">
<optionvalue=+>+</option>
<optionvalue=->-</option>
<optionvalue=*>*</option>
<optionvalue=/>/</option>
</select><br>
請輸入第二個數:<inputtype="text"name="num2"value="<%=strNum2%>"><br>
<inputtype=submitοnclick="returncheckNum()"value="等于">
</form>
結果:<%=strNum1%><%=operator%><%=strNum2%>=<%=result%>
</body>
</html>
java檢查字符串是否是合法的日期
ublicstaticbooleanisValidDate(Stringstr){booleanconvertSuccess=true;//指定日期格式為四位年/兩位月份/兩位日期,注意yyyy/MM/dd區分大小寫;SimpleDateFormatformat=newSimpleDateFormat("yyyy/MM/ddHH:mm");try{//設置lenient為false.否則SimpleDateFormat會比較寬松地驗證日期,比如2007/02/29會被接受,并轉換成2007/03/01format.setLenient(false);format.parse(str);}catch(ParseExceptione){//e.printStackTrace();//如果throwjava.text.ParseException或者NullPointerException,就說明格式不對convertSuccess=false;}returnconvertSuccess;}
thread.yield()是什么
在多線程程序中,為了防止某線程獨占CPU資源(這樣其它的線程就得不到"響應"了).可以讓當前執行的線程"休息"一下.但是這種thread.yield()調用,并不保證下一個運行的線程就一定不是該線程.可以考慮用Thread.sleep(longmillis);方法強制當前線程睡眠至少millis毫秒.但是使用時要對該方法捕獲.調用方法很簡單,只要在要睡眠的線程中加入Thread.yield();或者try{Thread.sleep(100);//睡眠100毫秒}catch(Exceptione){e.printStackTrace();}就行了.