-1

I am trying to read some book objects that I have stored in a txt file. When I run the code it runs but returns incorrect information.

Expected result: Book isbn=225346873945, price=12.99, yearPublished=2015, title='Finders Fee'

Actual result: Book isbn=⸹㔬⁹敡牐畢汩獨敤㴲〱〬⁴, price=9.770698273454668E199, yearPublished=1633907817, title='捥映瑨攠坩汤❽਀䵂潯死楳扮㴲㈵㌴㘸㜳㤴㔬⁰物捥㴱㈮㤹Ⱐ祥慲'

The information in the txt file is correct but when it is displayed in the console it is incorrect. Ignoring the invalid while loop, how can I fix this code?

    try {
        bookFile.seek(0);
        for (Book bookObj : bookList) {
            String tempBook = bookObj.toString();
            bookFile.writeBytes(tempBook);
            bookFile.writeBytes(System.getProperty("line.separator"));
        }
    } catch (IOException e) {
        System.out.println("Error writing to " + BOOK_LIST_FILE);
    } catch (Exception e) {
        System.out.println("Generic error" + e);
    }


try{
        Book tempBook = new Book();
        bookFile.seek(0);
        while (true) {
            readData(bookFile, tempBook);
            System.out.println(tempBook.toString());
        }
    } catch (IOException e){
        System.out.println("IO Error " + e);
    } catch (Exception e) {
        System.out.println("Error " + e.getMessage());
    }
}

public static void readData( RandomAccessFile bookFile, Book book) throws IOException, EOFException, Exception
{
    StringBuffer sb = new StringBuffer();

    for (int i=0; i<book.getISBN_LENGTH(); i++){
        sb.append(bookFile.readChar());
    }

    book.setIsbn(sb.toString());

    book.setPrice(bookFile.readDouble());

    book.setYearPublished(bookFile.readInt());

    sb.setLength(0);

    for (int i = 0; i <book.TITLE_LENGTH ; i++) {
        sb.append(bookFile.readChar());
    }

    book.setTitle(sb.toString());

}
  • 2
    How did you write the data? If you didn't use RandomAccessFile and the opposite write* methods to write the data, then you're likely reading a text file with a binary data reader. Not a good idea. Share the code that you used to write the data. You can look into the `Scanner` class for reading various data types from a text file. – Erwin Bolwidt May 01 '18 at 01:36
  • Updated the post to show the write method. – Sam Roehrich May 01 '18 at 01:39
  • You method for writing does not match the method for reading – Scary Wombat May 01 '18 at 01:52
  • Ok, you're writing out a string in your platform default encoding, which likely has one byte per character, and you're reading them using `readChar` which reads two bytes per character, and readDouble/readInt which read binary data. You need to write your data with the corresponding write methods (`writeChar`, `writeDouble`, `writeInt`) or you need to completely switch to use PrintWriter and Scanner to write and read data. – Erwin Bolwidt May 01 '18 at 01:52

1 Answers1

0

readInt() and friends are for binary data. Your file is text. You should just read bytes from this file. RandomAccessFile is a poor choice: use BufferedReader over an InputStreamReader over a FileInputStream, and the readLine() method.

user207421
  • 305,947
  • 44
  • 307
  • 483