-2

lets say i have a program that makes a binary file called data.bin and i write a bunch of random information to it using data.write(item) like:

  RandomAccessFile data = new RandomAccessFile("data.bin","rws");
  Random r = new Random(482010);
  Scanner input = new Scanner(System.in);
  int number = 100;
  byte[] item = new byte[1024];

  System.out.print("How many items (perhaps 800000)\n> ");
  number = input.nextInt();

  for (int i=0; i<number; i++) {
     r.nextBytes(item);
     data.write(item);
  }
  data.close();
    System.out.println("Done");

now once that file is completely written, if i do data.write(item) again does it overwrite all the information in that file? or does it keep adding on to the end of it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MadeInJapan
  • 45
  • 1
  • 8
  • 2
    Why don't you test it yourself and see, or see if you can't find the answer in [the documentation](http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#write%28byte[]%29)? – Matt Ball May 08 '13 at 03:41
  • I wouldn't use "rws" as this is quite a bit slower than "rw" and I don't see how it would help you. I would use plain FileOutputStream as this makes it quite clear whether you are overwriting or appending. – Peter Lawrey May 08 '13 at 05:31
  • As you are using the same random seed each time, you should be writing the same random data, so if you were overwriting the file would also look exactly the same for the same length. – Peter Lawrey May 08 '13 at 05:32

1 Answers1

0

Yes, since u have closed the file, the current position goes to the start of the file and now if you say data.write the data gets overwritten. If you have not closed the file then when you say data.write it continues at the end of file.

saranyakl
  • 54
  • 2