質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top