什么时候在java中使用flush()? [英] When to use flush() in java?

查看:317
本文介绍了什么时候在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.


  1. 何时使用flush方法,为什么要使用它?

  2. close方法带什么?这里得分?

  3. myObj2 =(John)ois.readObject(); ...如果我错了请纠正我,我正在读取文件对象并存储到另一个对象并对文件对象进行类型转换。

  4. 序列化的替代方法是什么,或者用Java保存数据。我不希望数据作为字节流进入文件。

  1. When to use flush method and why do we use it?
  2. What does close method carry a score here?
  3. 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.
  4. 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.


推荐答案


何时使用flush方法,我们为什么要使用它?

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

它刷新任何仍由的OutputStream 。详细说明见 JavaDoc

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


close方法在这里得分是多少?

What does close method carry a score here?

我不确定你的'得分'是什么意思,但关闭方法最终关闭资源(输入或输出),它释放例如文件句柄。你应该总是在 finally 块中调用close来清理操作系统可能拥有的所有引用。

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(); ...请纠正我,如果我错了,
i正在读取文件对象并存储到另一个对象中并且
类型化文件对象。

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.

不知道如何更正,你反序列化之前写入文件的对象。这些文件是专有的,只能由Java理解,所以你最后一个问题是一个!

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!



Java中序列化或持久化数据有哪些替代方法。我不希望数据作为字节流进入文件。

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.

你有很多选择。对于客户端应用程序,您可能希望使用XML,JSON或轻量级数据库(如SQLlite)。在服务器端,您也可以查看更强大的数据库(例如MySQL)。

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.

这篇关于什么时候在java中使用flush()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆