How to fix Stream Closed Exception
Asked Answered
E

2

5

i m working a small program that i can write a list into json file so i created two instance of Dealer class and then added it to the "dealerList".

I've tried to write into the json file named "Dealer.json" so i m reading the objects from a list named "dealerList" and then write it into the json file.

public class Main {
    private static List<Dealer> dealerList = new ArrayList<>();

    public static void main(String[] args) {
            Dealer dearler = new Dealer("Chevrolet");
            dearler.addCartoDealer(new Cars("Camaro","Steve",11000));
            dearler.addCartoDealer(new Cars("Coverette","Jhon",22000));
            Dealer dearler1 = new Dealer("Ford");
            dearler1.addCartoDealer(new Cars("Ford1","Jessie",11000));
            dearler1.addCartoDealer(new Cars("Ford2","Smith",22000));
            dealerList.add(dearler);
            dealerList.add(dearler1);
            ObjectMapper mapper = new ObjectMapper();

            try(FileOutputStream newFile = new FileOutputStream("Dealer.json")){
               for(Dealer dealer:dealerList){
                  mapper.writeValue(newFile,dealer);
               }
            } catch (IOException e){
              e.printStackTrace();
            }
    }

the stack i get:

java.io.IOException: Stream Closed
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.java:326)
    at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2093)
    at com.fasterxml.jackson.core.json.UTF8JsonGenerator.close(UTF8JsonGenerator.java:1137)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3983)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3245)
    at com.Ayoub.Main.main(Main.java:26)

i got the Stream closed excepetion

Ediva answered 25/7, 2019 at 15:8 Comment(0)
T
4

I think Jackson is auto closing your stream after the call to writeValue. You can turn this off via:

MessagePackFactory messagePackFactory = new MessagePackFactory();
messagePackFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);

Don't forget to close the stream yourself at some point in time ;-)

Taligrade answered 25/7, 2019 at 15:15 Comment(2)
is the MessagePackFactory a part of jackson package coz the intellij doesn't seem to import it if you can provide me with the packageEdiva
@souid wejden you can also configure this feature on the default JsonFactory like this: objectMapper.getFactory().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); . You can also use any other JsonFactory implementation if you want, depending on your needs.Boschbok
E
2

Just found it should added:

JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET,false);
ObjectMapper mapper = new ObjectMapper(jsonFactory);
Ediva answered 25/7, 2019 at 15:29 Comment(1)
Didn't work, it is still throwing this exceptionProtuberancy

© 2022 - 2024 — McMap. All rights reserved.