Hey, I am having a problem figuring this out. I didn't use file I/O before this so be gentle at me :P.

Here is my problem: I am writing to a file, an object Book. The constructor looks as follows:

public Book(float bookId, String bkName, String bkAuthor, int year, String desc, boolean series)

My problem is, I want to write to a file, and use random access when searching through. How can I do this if the name/author/description will potentially be a different size for each book? And therefore, each object will be larger/smaller? Is it possible to use random file access for objects of different size?

Am I even thinking about this right?

Sorry, I just thought, would a good solution be to give each object a maximum size or something? Or give each String a max size? How would I do this?

Can somebody please reply and help me? I have gotten so far as it can add multiple records, but, when I retrieve them, the later record always overwrites the previous. So only the last record is intact. This is the method I use to write to the file:

public int addBook(Book p) throws LibraryException {
        RandomAccessFile rw = random.getRandomAccessRW(libraryFile);
        int records=0;
        try {
            records = getNumRecords(rw);

            rw.seek((records)*RECORD_SIZE);
            
            rw.writeFloat(p.get_ID());
            BookUtility.writeFixedString(rw, NAME_SIZE, p.get_name());
            BookUtility.writeFixedString(rw, AUTHOR_SIZE, p.get_author());
            rw.writeInt(p.get_year());
            BookUtility.writeFixedString(rw, DESC_SIZE, p.get_description());
           // rw.writeBoolean(p.get_isSeries());
              random.close(rw);
            return 1;
        } catch (IOException ex) {
           System.out.println("Problem writing to random access fille" + ex.getMessage());
        }
        return 0;
        
    }

WriteFixedString is a method I wrote here:

public static void writeFixedString(RandomAccessFile out, int length, String s)throws IOException
    {
         for(int i=0;i<length;i++)
         {
             if(i<s.length())
                 out.writeChar(s.charAt(i));
            else
                out.writeChar(0);
         }

     }

Can somebody please help me out here?

EDIT: I suppose my problem could actually be when I try to read from the file(maybe I am doing this bit wrong)....here is where I read from file:

public List<Book> findAll() throws LibraryException {
        RandomAccessFile r = random.getRandomAccessRead(libraryFile);
        Book book = null;
        List<Book> books = new ArrayList<Book>();

        try {
             int records = getNumRecords(r);
             for(int i=0;i<records;i++)
             {
                 r.seek(i*RECORD_SIZE);
                 float id = r.readFloat();
                 String name = BookUtility.getFixedString(r,NAME_SIZE);
                 String author  = BookUtility.getFixedString(r,AUTHOR_SIZE);
                 int year = r.readInt();
                 String desc   = BookUtility.getFixedString(r,DESC_SIZE);
                // boolean series = r.readBoolean();
                 book = new Book(id,name,author,year,desc,true);
                 books.add(book);
             }
        } catch (IOException ex) {
            Logger.getLogger(LibraryRandomAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return books;
    }

And the getFixedString method I use:

public static String getFixedString(RandomAccessFile in, int length)throws IOException
    {
         StringBuilder sb = new StringBuilder();
         for(int i=0;i<length;i++)
         {
             char c = in.readChar();
             if(c!=0)
                 sb.append(c);
         }
         return sb.toString();

     }

Again, any help is appreciated

Edited 5 Years Ago by Javam: n/a

Ok, I figured out what was wrong, I calculated the record size wrong, which I do not understand. I was 150bytes out. This is what I was using:

private final int NAME_SIZE = 50;
    private final int AUTHOR_SIZE = 30;
    private final int DESC_SIZE = 70;
    private final int RECORD_SIZE = 4 + NAME_SIZE + AUTHOR_SIZE+4+DESC_SIZE;//+0.125;

this adds to 158. I checked the file size after 1 record, and it was 308 bytes. I can change the RECORD_SIZE=308. But I want to know how I am missing 150 bytes

What am I missing here?


Ah Yes of course, My write fixed string writes out chars, and therefore doubles the size!! So, doubling 50,30 and 70 will give me the correct value.


Well, thanks to myself I guess.

Edited 5 Years Ago by Javam: n/a

This question has already been answered. Start a new discussion instead.