Search code examples
javainputmxml

How to take in input and show an mxml file in java


I wrote this simple code but i can't understand why it keeps returning me errors, Can you help me?

{
/*XesXmlParser parser = new XesXmlParser();*/
XMxmlParser parser = new XMxmlParser();
InputStream is = null;

try {
    is = new FileInputStream("C:\\Users\\examplefolder\\prova1.mxml");
    parser.parse(is);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
java.util.List<XLog> list = parser.parse(is);
System.out.println(list);


}

it gives me this error:

{Exception in thread "main" java.io.IOException: Stream Closed
...
at org.deckfour.xes.in.XMxmlParser.parse(XMxmlParser.java:196)
at provaletturalog.LeggiLog.main(LeggiLog.java:26)

Solution

  • The second parser.parse(is) call is causing the error because the stream has already been consumed by the previous method call.

    So you need something like this:

    XMxmlParser parser = new XMxmlParser();
    List<XLog> list = Collections.emptyList();
    try (InputStream is = new FileInputStream("C:\\Users\\examplefolder\\prova1.mxml")) {
        list = parser.parse(is);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    System.out.println(list);
    

    I've used try with resources construction as streams should be closed to avoid leaks.