In: Computer Science
When using random access files in Java, the programmer is permitted to create files which can be read from and written to random locations. Assume that a file called "integers.dat" contains the following values: 25 8 700 284 63 12 50 Fill in blanks: You are required to write the following Java code statements: (a) create a new stream called, blueray, which allows the program to read from and write to the file, "integers.dat." _____________________ (b) Write a statement which will move the file pointer to the number, 700. _____________________ (c) Write a statement to display the contents currently pointed to. _____________________ (d) Write a statement which will move the file pointer from its current position, directly to the end of the file. _____________________ (e) Append the value 999999 to the file. _____________________
(a) create a new stream called, blueray, which allows the program to read from and write to the file, "integers.dat."
RandomAccessFile blueray = new RandomAccessFile("E:/chegg/integers.dat", "rw");
Create a new RandomAccessFile stream specifying path of the file and the mode of file
(b) Write a statement which will move the file pointer to the number, 700.
blueray.seek(2);
seek() helps in setting the file pointer. Since 700 is found at 2nd we'll set seek to 2.
(c) Write a statement to display the contents currently pointed to.
System.out.println("" + blueray.readInt());
readInt() helps in reading the integers from file and println statement displays them
(d) Write a statement which will move the file pointer from its current position, directly to the end of the file.
blueray.seek(blueray.length());
length() gives the length of file, and use seek() to point the it => points to the end of file
(e) Append the value 999999 to the file.
blueray.writeInt(9999999);
writes 999999 to the location pointed by the file pointer