How to write serialized objects to XML files without overwriting? SAXParseException

0

So, as the title suggests, I've been trying to write serialized objects without overwriting the file. The problem is that whenever I add an object to this file I get the following sentence:

"org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file. Continuing ... "

And then nothing is written to the file. If I try to serialize the objects into a file other than XML, let's assume the type .ser, returns me an error saying that it could not be written to the file.

 public static void addRecords() throws IOException {

    inpt = new XMLDecoder(
            new BufferedInputStream(
                    new FileInputStream("library.xml")));
    ArrayList<Book> books;

    Scanner input = new Scanner(System.in);

    System.out.printf("%s%n%s%n? ",
            "Enter ISBN number, Author's name, Book's Title.",
            "Enter end-of-file indicator to end input.");

    while (input.hasNext()) {

        try {
            Book record = new Book(input.nextLine(),
                    input.nextLine(), input.nextLine());

            books = (ArrayList<Book>) inpt.readObject(); //Aqui o Intellij "seleciona" essa linha e me diz o seguinte:"Unchecked cast: 'java.lang.Object' to 'java.util.ArrayList'"
            inpt.close();
            books.add(record);
            output.writeObject(books);

        } catch (NullPointerException e){
            e.getStackTrace();

        } catch (ArrayIndexOutOfBoundsException arrayExc){

        }
        System.out.println("? ");
    }
}
    
asked by anonymous 13.09.2018 / 22:05

0 answers