简体   繁体   English

在Java中,如何将对象写入文件,然后再从文件中读取该对象并将其转换回HDFS中的原始对象?

[英]In java, how to write an object to a file and later on read it from the file and convert it back to the original object in the HDFS?

I am trying to write a double[] into a file in HDFS, and later on, I will need to read it back from the file and convert it back to the double[]. 我试图将double []写入HDFS的文件中,以后,我需要从文件中读取回去并将其转换回double []。 Does anyone here know how to do it? 这里有人知道怎么做吗?

Thanks, 谢谢,

ObjectOutputStream ObjectOutputStream

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. ObjectOutputStream将Java对象的原始数据类型和图形写入OutputStream。 The objects can be read (reconstituted) using an ObjectInputStream. 可以使用ObjectInputStream读取(重构)对象。 Persistent storage of objects can be accomplished by using a file for the stream. 可以通过使用流文件来实现对象的持久存储。 If the stream is a network socket stream, the objects can be reconstituted on another host or in another process. 如果流是网络套接字流,则可以在另一台主机上或另一进程中重构对象。

Only objects that support the java.io.Serializable interface can be written to streams. 只有支持java.io.Serializable接口的对象才能写入流。 The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects. 每个可序列化对象的类都经过编码,包括类名称和类签名,对象的字段和数组的值以及从初始对象引用的任何其他对象的关闭。

The method writeObject is used to write an object to the stream. writeObject方法用于将对象写入流。 Any object, including Strings and arrays, is written with writeObject. 任何对象(包括字符串和数组)都是使用writeObject编写的。 Multiple objects or primitives can be written to the stream. 可以将多个对象或基元写入流。 The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written. 必须从相应的ObjectInputstream中以与写入对象相同的类型和顺序读取对象。

Primitive data types can also be written to the stream using the appropriate methods from DataOutput. 还可以使用DataOutput中的适当方法将原始数据类型写入流中。 Strings can also be written using the writeUTF method. 也可以使用writeUTF方法写入字符串。

ObjectInputStream ObjectInputStream

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. ObjectInputStream反序列化原始数据和先前使用ObjectOutputStream写入的对象。

ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. 当分别与FileOutputStream和FileInputStream一起使用时,ObjectOutputStream和ObjectInputStream可以为应用程序提供对象图的持久存储。 ObjectInputStream is used to recover those objects previously serialized. ObjectInputStream用于恢复先前序列化的那些对象。 Other uses include passing objects between hosts using a socket stream or for marshaling and unmarshaling arguments and parameters in a remote communication system. 其他用途包括使用套接字流在主机之间传递对象,或在远程通信系统中用于编组和解组参数和参数。

ObjectInputStream ensures that the types of all objects in the graph created from the stream match the classes present in the Java Virtual Machine. ObjectInputStream确保从流创建的图中的所有对象的类型与Java虚拟机中存在的类匹配。 Classes are loaded as required using the standard mechanisms. 使用标准机制根据需要加载类。

For example, I can save a key generator for encrypting data in this manner: 例如,我可以保存密钥生成器以这种方式加密数据:

public static void saveKeyToFile(SecretKey key)
        throws FileNotFoundException, IOException {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
            "/path/to/mysavedobject"));
    oos.writeObject(key);
    oos.close();
}

public static SecretKey getKeyFromFile(String dir) throws IOException,
        ClassNotFoundException {
    if (dir == null) { 
        dir = "/path/to/mysavedobject";
    }
    SecretKey key = null;
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
            dir));
    key = (SecretKey) ois.readObject();
    ois.close();
    return key;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM