java什么时候炎flush,什么时候在java中使用flush()?

import java.io. * ;

public class Ser {

public static void main(String args[]) {

try {

John myObj = new John("Sachin", "Cricket");

System.out.println(myObj);

FileOutputStream fos = new FileOutputStream("FileName");

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(myObj);

oos.flush();

oos.close();

} catch (Exception e) {

System.out.println("Expection" + e);

System.exit(0);

}

try {

John myObj2;

FileInputStream fis = new FileInputStream("FileName");

ObjectInputStream ois = new ObjectInputStream(fis);

myObj2 = (John) ois.readObject();

ois.close();

System.out.println("New Object" + myObj2);

} catch (Exception e) {

System.out.println("Expection" + e);

System.exit(0);

}

}

}

class John implements Serializable {

private String name;

private String department;

public John(String name, String department) {

this.name = name;

this.department = department;

}

public String toString() {

return "Name" + name + " " + "Department" + department;

}

}

I have a few questions in the above example.

When to use flush method and why do we use it?

What does close method carry a score here?

myObj2 = (John) ois.readObject(); ... please correct me if i am wrong, i am reading the file object and storing into another object and typecasting the file object.

What are the alternatives of Serialization or persisting the data in Java. I don't want the data to get into file as byte stream.

解决方案

When to use flush method and why do we use it?

It flushes anything which is still buffered by the OutputStream. A detailed description is available in JavaDoc.

What does close method carry a score here?

I'm not sure what you mean by 'carry a score', but the close method finally closes resources (Input or Output), it releases e.g. file handles. You should always call close in a finally block to clean up all references the OS may have.

InputStream is = null;

try {

is = new FileInputStream(...);

// do stuff

} catch (IOException e) {

// do stuff

} finally {

if (is != null) {

is.close();

}

}

myObj2 = (John) ois.readObject(); ... please correct me if i am wrong,

i am reading the file object and storing into another object and

typecasting the file object.

Somehow correct, you deserialize an Object writen to the file before. Those files are proprietary and can only be understood by Java, so you last question is a good one!

What are the alternatives of Serialization or persisting the data in

Java. I don't want the data to get into file as byte stream.

You have a number of options. For client application you may want to use XML, JSON, or a lightweight database like SQLlite. On server-side you may have a look at more robust databases (e.g. MySQL) as well.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值