0

Here the registration number is given as 100, what I want is;

Instead of determining the number of records from the beginning, I want the number of records to increase as I add records.

I will exemplify what I want to tell through arrays;

Not so;

Array[100];
Array[0]=data;

Like this;

Array[];
Array.push(data);
import com.deitel.jhtp6.ch24.RandomAccessAccountRecord;
import java.io.IOException;
import java.io.RandomAccessFile;

public class CreateRandomFile {
    private static final int NUMBER_RECORDS = 100;

    public void createFile() {
        RandomAccessFile file = null;
        try {
            file = new RandomAccessFile("clients.dat", "rw");
            RandomAccessAccountRecord blankRecord = new RandomAccessAccountRecord();
            for (int count = 0; count < NUMBER_RECORDS; count++) 
                blankRecord.write(file);
            System.out.println("Created file clients.dat.");
            System.exit(0);
        } catch (IOException ioException) {
            System.err.println("Error processing file.");
            System.exit(1);
        } finally {
            try {
                if (file != null) 
                    file.close();
            } catch (IOException ioException) {
                System.err.println("Error closing file.");
                System.exit(1);
            }
        }
    }
}

It can be done by counting the number of recordings and adding +1 each time, but this is not practical or professional at all.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    Please [edit] your source code to fix the indentation. It is difficult to see which block starts where and where it ends. – Progman Dec 30 '22 at 13:27
  • i'm unclear on your question. are you asking how to append records to the file instead of replacing the contents each time? if so then you'll need to `.seek(..)` to the end of the file after you open it – Stik Dec 30 '22 at 13:37
  • See this post with an implementation https://stackoverflow.com/questions/199847/random-access-file-in-java – Stu Dec 30 '22 at 13:49
  • @stu It creates a fixed size here, so it's the opposite of what I said. – CagatayAksoy Dec 30 '22 at 14:01

1 Answers1

1

If you don't know the number of the records in advance, then you should not use a for loop, but rather a different construct. A while loop with a check if there are still accounts records to write to file left would do for example. I don't understand the code logic though, you initialise one instance of RandomAccessAccountRecord blankRecord = new RandomAccessAccountRecord(); and then call the write method on the same instance hundred times?

I don't get this one either:

Not so; Array[100]; Array[0]=data;

Like this; Array[]; Array.push(data);

Does that mean that you need a data structure which will be initialised as empty and you would dynamically add elements? Then array is not the best choice for that.

Pavel
  • 785
  • 5
  • 14
  • YES; (Does that mean that you need a data structure which will be initialised as empty and you would dynamically add elements? ) No; (Then array is not the best choice for that.) I just gave an example using arrays to describe what I want. – CagatayAksoy Dec 30 '22 at 13:49
  • Another example; A chair will be placed in a classroom, It is unknown how many students are in this class. I mean; As students come to the classroom (as new records are added) a chair should be placed (the database inflates). – CagatayAksoy Dec 30 '22 at 13:56
  • So you need a dynamic data structure. Array is not the good fit for that as you need to initialise with some length and once you hit the limit, you need to resize it, which means creating a new array and copying everything. There are other things to consider when choosing the correct data structure, do you need a random access to any element based on index or you'll be just appending and reading from its head or the last element? I suggest you check on data structures like linked list, queue, stack etc. – Pavel Dec 30 '22 at 14:12
  • 1
    *As students come to the classroom (as new records are added) a chair should be placed* Solution: *append* a record to a file. `file.seek(file.length());filledRecord.write(file);` – g00se Dec 30 '22 at 14:45
  • @g00se Yes it can be like this So, can this be solved with a database management system? In other words, we will send the data and the database will detect this and create +1 more empty record and fill it. – CagatayAksoy Dec 30 '22 at 16:35
  • Well...yes. That's how a SQL insert works – g00se Dec 30 '22 at 16:47
  • So how should i use (file.seek) then. (without defining size). I can't find any sample code on the internet for this exact issue. What should I call? – CagatayAksoy Dec 30 '22 at 17:14
  • I don't know what you mean by *without defining size*. All files have a size - even if it's zero – g00se Dec 30 '22 at 17:47
  • No. So without this definition; private static final int NUMBER_RECORDS = 100; – CagatayAksoy Dec 30 '22 at 18:07
  • You don't need that defined at all . Just use the code i showed you – g00se Dec 30 '22 at 19:09