import java.io.*;
public class SerializationDemo{
public static void main(String args[]){
//Object serialization
try{
MyClass object1=new MyClass("Hello",-7,2.7e10);
System.out.println("object1:"+object1);
FileOutputStream fos=new FileOutputStream("serial");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e){
System.out.println("Exception during serialization:"+e);
System.exit(0);
}
//Object deserialization
try{
MyClass object2;
FileInputStream fis=new FileInputStream("serial");
ObjectInputStream ois=new ObjectInputStream(fis);
object2=(MyClass)ois.readObject();
ois.close();
System.out.println("object2:"+object2);
}
catch(Exception e){
System.out.println("Exception during deserialization:"+e);
System.exit(0);
}
}
}
class MyClass implements Serializable{
String s;
int i;
double d;
public MyClass(String s,int i,double d){
this.s=s;
this.i=i;
this.d=d;
}
public String toString(){
return "s="+s+";i="+i+";d="+d;
}
}
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @author Yan Chenyang
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class JFunction implements Serializable{
private double[][] bounds;
private int vnum;
private double result;
private String funcname;
public JFunction(){
super();
this.bounds=null;
this.vnum=0;
this.result=0.0;
this.funcname=null;
}
public JFunction(double[][] bounds){
super();
this.bounds=bounds;
this.vnum=bounds[0].length;
this.result=0.0;
this.funcname="Function 1";
}
public double Func(double[] var){
result=0.0;
for(int i=0;i<var.length;i++)
result+=var[i];
return result;
}
public static JFunction deserialize(ObjectInputStream oin)
throws Exception{
JFunction f=(JFunction)oin.readObject();
return f;
}
public void serialize(ObjectOutputStream oout)
throws Exception{
oout.writeObject(this);
}
public String toString(){
return funcname ;
}
/**
* @return 返回 funcname。
*/
public String getFuncname() {
return funcname;
}
/**
* @param funcname 要设置的 funcname。
*/
public void setFuncname(String funcname) {
this.funcname = funcname;
}
}
public class JFunctionTest0 {
public static void main(String[] args){
double[][] bounds={{-2.048,-2.048},{2.048,2.048}};
JFunction function=new JFunction(bounds);
Interpreter interpreter=new Interpreter();
try {
double[] x={2.15,1.00};
interpreter.set("var",x);
interpreter.source("c:/x.bsh");
System.out.println(interpreter.get("ret"));
} catch (EvalError e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
FileInputStream in=null;
FileOutputStream out=null;
ObjectInputStream oin=null;
ObjectOutputStream oout=null;
try{
out = new FileOutputStream("function1.func");
oout = new ObjectOutputStream(out);
function.serialize(oout);//序列化
oout.close();
oout=null;
in = new FileInputStream("function1.func");
oin = new ObjectInputStream(in);
JFunction tfunction =JFunction.deserialize(oin);//反序列化
double[] var={2.0,5.0};
// System.out.println(tfunction.Func(var));
// System.out.println(tfunction);//打印结果
//
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
if (in != null) {
in.close();
}
if (oin != null) {
oin.close();
}
if (out != null) {
out.close();
}
if (oout != null) {
oout.close();
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有