java数据保存到文件,Java - 将对象数据保存到文件

I've seen so many different posts about what way you're supposed to serialize an object to a file, and all of them conflict in nature on how to do it and what the best practices are. So here's what I'm trying to save:

public class IHandler{

public double currentLoad;

public String currentPrice;

public String configArgs[];

};

We can assume that the size of configArgs is known that I need to make a file, here's what I have so far.

public static void serializeDataOut(IHandler ISH)throws IOException{

String fileName= "Test.txt";

FileOutputStream fos = new FileOutputStream(fileName);

//What do I do here?

}

public static IHandler serializeDataIn(){

//What do I do here?

}

解决方案

Well I assume, you want to write object directly into the file

public static void serializeDataOut(IHandler ish)throws IOException{

String fileName= "Test.txt";

FileOutputStream fos = new FileOutputStream(fileName);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(ish);

oos.close();

}

public static IHandler serializeDataIn(){

String fileName= "Test.txt";

FileInputStream fin = new FileInputStream(fileName);

ObjectInputStream ois = new ObjectInputStream(fin);

IHandler iHandler= (IHandler) ois.readObject();

ois.close();

return iHandler;

}

I just provided important code. Implement this with exception handling.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值