How to read all objects from ObjectInputStream












0















I have a file with some info how can I read all info?



Name names;    
try (FileInputStream fileInputStream = new FileInputStream(file)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
names = (Name) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}









share|improve this question

























  • please check this link to know how to ask a question.

    – JSmith
    Nov 25 '18 at 12:01











  • If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

    – Sid
    Nov 25 '18 at 12:04











  • I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

    – Oppo
    Nov 25 '18 at 12:09
















0















I have a file with some info how can I read all info?



Name names;    
try (FileInputStream fileInputStream = new FileInputStream(file)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
names = (Name) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}









share|improve this question

























  • please check this link to know how to ask a question.

    – JSmith
    Nov 25 '18 at 12:01











  • If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

    – Sid
    Nov 25 '18 at 12:04











  • I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

    – Oppo
    Nov 25 '18 at 12:09














0












0








0








I have a file with some info how can I read all info?



Name names;    
try (FileInputStream fileInputStream = new FileInputStream(file)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
names = (Name) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}









share|improve this question
















I have a file with some info how can I read all info?



Name names;    
try (FileInputStream fileInputStream = new FileInputStream(file)) {
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
names = (Name) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}






java file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:02









JSmith

1,49311226




1,49311226










asked Nov 25 '18 at 11:51









OppoOppo

85




85













  • please check this link to know how to ask a question.

    – JSmith
    Nov 25 '18 at 12:01











  • If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

    – Sid
    Nov 25 '18 at 12:04











  • I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

    – Oppo
    Nov 25 '18 at 12:09



















  • please check this link to know how to ask a question.

    – JSmith
    Nov 25 '18 at 12:01











  • If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

    – Sid
    Nov 25 '18 at 12:04











  • I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

    – Oppo
    Nov 25 '18 at 12:09

















please check this link to know how to ask a question.

– JSmith
Nov 25 '18 at 12:01





please check this link to know how to ask a question.

– JSmith
Nov 25 '18 at 12:01













If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

– Sid
Nov 25 '18 at 12:04





If you are referring to serialization, I believe one stream can be one object. It can be a composite of multiple object (e.g. ArrayList), but a single object nevertheless.

– Sid
Nov 25 '18 at 12:04













I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

– Oppo
Nov 25 '18 at 12:09





I tried add objects into HashSet but had an exception java.io.StreamCorruptedException: invalid type code: AC. I input name using scanner and than I need to check if name already exist.

– Oppo
Nov 25 '18 at 12:09












1 Answer
1






active

oldest

votes


















0














You have several solution, all depending on the input:





  • You can iterate until the stream is fully consumed: I think that is the worse solution out of those I provide you. It is worse because you are checking if EOF was reached, whilst you should know when you're done (eg: your file format is wrong).



    Set<Name> result = new HashSet<>();
    try {
    for (;;) {
    result.add((Name)objectInputStream.readObject());
    }
    } catch (EOFException e) {
    // End of stream
    }
    return result;



  • When producing the input, serialize a collection and invoke readObject() on it. Serialization should be able to read the collection, as long as each object implements Serializable.



    static void write(Path path, Set<Name> names) throws IOException {
    try (OutputStream os = Files.newOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(os)) {
    oos.writeObject(names);
    }
    }

    static Set<Name> read(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(is)) {
    // WARN Files.newInputStream is not buffered; ObjectInputStream might
    // be buffered (I don't remember).
    return (Set<Name>) ois.readObject();
    }
    }



  • When producing the input, you can add a int indicating the number of object to read, and iterate over it: this is useful in case where you don't really care of the collection (HashSet). The resulting file will be smaller (because you won't have the HashSet metadata).



    int result = objectInputStream.readInt();
    Name names = new Name[result]; // do some check on result!
    for (int i = 0; i < result; ++i) {
    names[i] = (Name) objectInputStream.readObject();
    }



Also, Set are good, but since they remove duplicate using hashCode()/equals() you may get less object if your definition of equals/hashCode changed after the fact (example: your Name was case sensitive and now it is not, eg: new Name("AA").equals(new Name("aa"))).






share|improve this answer


























  • I tried your code and I got an java.io.EOFException

    – Oppo
    Nov 25 '18 at 12:21











  • That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

    – Themelis
    Nov 25 '18 at 12:22











  • And instead of array I can use HashSet? and print it to console?

    – Oppo
    Nov 25 '18 at 12:28











  • I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

    – NoDataFound
    Nov 25 '18 at 18:34













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467133%2fhow-to-read-all-objects-from-objectinputstream%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have several solution, all depending on the input:





  • You can iterate until the stream is fully consumed: I think that is the worse solution out of those I provide you. It is worse because you are checking if EOF was reached, whilst you should know when you're done (eg: your file format is wrong).



    Set<Name> result = new HashSet<>();
    try {
    for (;;) {
    result.add((Name)objectInputStream.readObject());
    }
    } catch (EOFException e) {
    // End of stream
    }
    return result;



  • When producing the input, serialize a collection and invoke readObject() on it. Serialization should be able to read the collection, as long as each object implements Serializable.



    static void write(Path path, Set<Name> names) throws IOException {
    try (OutputStream os = Files.newOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(os)) {
    oos.writeObject(names);
    }
    }

    static Set<Name> read(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(is)) {
    // WARN Files.newInputStream is not buffered; ObjectInputStream might
    // be buffered (I don't remember).
    return (Set<Name>) ois.readObject();
    }
    }



  • When producing the input, you can add a int indicating the number of object to read, and iterate over it: this is useful in case where you don't really care of the collection (HashSet). The resulting file will be smaller (because you won't have the HashSet metadata).



    int result = objectInputStream.readInt();
    Name names = new Name[result]; // do some check on result!
    for (int i = 0; i < result; ++i) {
    names[i] = (Name) objectInputStream.readObject();
    }



Also, Set are good, but since they remove duplicate using hashCode()/equals() you may get less object if your definition of equals/hashCode changed after the fact (example: your Name was case sensitive and now it is not, eg: new Name("AA").equals(new Name("aa"))).






share|improve this answer


























  • I tried your code and I got an java.io.EOFException

    – Oppo
    Nov 25 '18 at 12:21











  • That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

    – Themelis
    Nov 25 '18 at 12:22











  • And instead of array I can use HashSet? and print it to console?

    – Oppo
    Nov 25 '18 at 12:28











  • I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

    – NoDataFound
    Nov 25 '18 at 18:34


















0














You have several solution, all depending on the input:





  • You can iterate until the stream is fully consumed: I think that is the worse solution out of those I provide you. It is worse because you are checking if EOF was reached, whilst you should know when you're done (eg: your file format is wrong).



    Set<Name> result = new HashSet<>();
    try {
    for (;;) {
    result.add((Name)objectInputStream.readObject());
    }
    } catch (EOFException e) {
    // End of stream
    }
    return result;



  • When producing the input, serialize a collection and invoke readObject() on it. Serialization should be able to read the collection, as long as each object implements Serializable.



    static void write(Path path, Set<Name> names) throws IOException {
    try (OutputStream os = Files.newOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(os)) {
    oos.writeObject(names);
    }
    }

    static Set<Name> read(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(is)) {
    // WARN Files.newInputStream is not buffered; ObjectInputStream might
    // be buffered (I don't remember).
    return (Set<Name>) ois.readObject();
    }
    }



  • When producing the input, you can add a int indicating the number of object to read, and iterate over it: this is useful in case where you don't really care of the collection (HashSet). The resulting file will be smaller (because you won't have the HashSet metadata).



    int result = objectInputStream.readInt();
    Name names = new Name[result]; // do some check on result!
    for (int i = 0; i < result; ++i) {
    names[i] = (Name) objectInputStream.readObject();
    }



Also, Set are good, but since they remove duplicate using hashCode()/equals() you may get less object if your definition of equals/hashCode changed after the fact (example: your Name was case sensitive and now it is not, eg: new Name("AA").equals(new Name("aa"))).






share|improve this answer


























  • I tried your code and I got an java.io.EOFException

    – Oppo
    Nov 25 '18 at 12:21











  • That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

    – Themelis
    Nov 25 '18 at 12:22











  • And instead of array I can use HashSet? and print it to console?

    – Oppo
    Nov 25 '18 at 12:28











  • I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

    – NoDataFound
    Nov 25 '18 at 18:34
















0












0








0







You have several solution, all depending on the input:





  • You can iterate until the stream is fully consumed: I think that is the worse solution out of those I provide you. It is worse because you are checking if EOF was reached, whilst you should know when you're done (eg: your file format is wrong).



    Set<Name> result = new HashSet<>();
    try {
    for (;;) {
    result.add((Name)objectInputStream.readObject());
    }
    } catch (EOFException e) {
    // End of stream
    }
    return result;



  • When producing the input, serialize a collection and invoke readObject() on it. Serialization should be able to read the collection, as long as each object implements Serializable.



    static void write(Path path, Set<Name> names) throws IOException {
    try (OutputStream os = Files.newOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(os)) {
    oos.writeObject(names);
    }
    }

    static Set<Name> read(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(is)) {
    // WARN Files.newInputStream is not buffered; ObjectInputStream might
    // be buffered (I don't remember).
    return (Set<Name>) ois.readObject();
    }
    }



  • When producing the input, you can add a int indicating the number of object to read, and iterate over it: this is useful in case where you don't really care of the collection (HashSet). The resulting file will be smaller (because you won't have the HashSet metadata).



    int result = objectInputStream.readInt();
    Name names = new Name[result]; // do some check on result!
    for (int i = 0; i < result; ++i) {
    names[i] = (Name) objectInputStream.readObject();
    }



Also, Set are good, but since they remove duplicate using hashCode()/equals() you may get less object if your definition of equals/hashCode changed after the fact (example: your Name was case sensitive and now it is not, eg: new Name("AA").equals(new Name("aa"))).






share|improve this answer















You have several solution, all depending on the input:





  • You can iterate until the stream is fully consumed: I think that is the worse solution out of those I provide you. It is worse because you are checking if EOF was reached, whilst you should know when you're done (eg: your file format is wrong).



    Set<Name> result = new HashSet<>();
    try {
    for (;;) {
    result.add((Name)objectInputStream.readObject());
    }
    } catch (EOFException e) {
    // End of stream
    }
    return result;



  • When producing the input, serialize a collection and invoke readObject() on it. Serialization should be able to read the collection, as long as each object implements Serializable.



    static void write(Path path, Set<Name> names) throws IOException {
    try (OutputStream os = Files.newOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(os)) {
    oos.writeObject(names);
    }
    }

    static Set<Name> read(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(is)) {
    // WARN Files.newInputStream is not buffered; ObjectInputStream might
    // be buffered (I don't remember).
    return (Set<Name>) ois.readObject();
    }
    }



  • When producing the input, you can add a int indicating the number of object to read, and iterate over it: this is useful in case where you don't really care of the collection (HashSet). The resulting file will be smaller (because you won't have the HashSet metadata).



    int result = objectInputStream.readInt();
    Name names = new Name[result]; // do some check on result!
    for (int i = 0; i < result; ++i) {
    names[i] = (Name) objectInputStream.readObject();
    }



Also, Set are good, but since they remove duplicate using hashCode()/equals() you may get less object if your definition of equals/hashCode changed after the fact (example: your Name was case sensitive and now it is not, eg: new Name("AA").equals(new Name("aa"))).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 22:06

























answered Nov 25 '18 at 12:13









NoDataFoundNoDataFound

5,8211841




5,8211841













  • I tried your code and I got an java.io.EOFException

    – Oppo
    Nov 25 '18 at 12:21











  • That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

    – Themelis
    Nov 25 '18 at 12:22











  • And instead of array I can use HashSet? and print it to console?

    – Oppo
    Nov 25 '18 at 12:28











  • I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

    – NoDataFound
    Nov 25 '18 at 18:34





















  • I tried your code and I got an java.io.EOFException

    – Oppo
    Nov 25 '18 at 12:21











  • That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

    – Themelis
    Nov 25 '18 at 12:22











  • And instead of array I can use HashSet? and print it to console?

    – Oppo
    Nov 25 '18 at 12:28











  • I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

    – NoDataFound
    Nov 25 '18 at 18:34



















I tried your code and I got an java.io.EOFException

– Oppo
Nov 25 '18 at 12:21





I tried your code and I got an java.io.EOFException

– Oppo
Nov 25 '18 at 12:21













That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

– Themelis
Nov 25 '18 at 12:22





That's good, this means that you reached the end of the file, there are no more object to read. Handle this exception properly, like printing a message saying "All the objects read" or something...

– Themelis
Nov 25 '18 at 12:22













And instead of array I can use HashSet? and print it to console?

– Oppo
Nov 25 '18 at 12:28





And instead of array I can use HashSet? and print it to console?

– Oppo
Nov 25 '18 at 12:28













I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

– NoDataFound
Nov 25 '18 at 18:34







I fixed my example. Beside, converting an array to a collection should not be a problem if you are tackling serialization.

– NoDataFound
Nov 25 '18 at 18:34






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467133%2fhow-to-read-all-objects-from-objectinputstream%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen