Question

In: Computer Science

When using random access files in Java, the programmer is permitted to create files which can...

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. _____________________

Solutions

Expert Solution

(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


Related Solutions

describe the mechanism that supports random file access in Java.
describe the mechanism that supports random file access in Java.
(Using Java) create a class that can identify a palindrome. A palindrome is defined as -...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as - A string of characters that reads the same from left to right as its does from right to left - Example: Anna, Civic, Kayak, Level, Madam - To recognize a palindrome, a queue can be used in conjunction with a stack o A stack can be used to reverse the order of occurrences o A queue can be used to preserve the order of...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
When using Microsoft Visio or Access, what is the importance of data validation, and how can...
When using Microsoft Visio or Access, what is the importance of data validation, and how can user data entry errors be reduced or eliminated?
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”. Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem: a2 + b2 = c2. This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle. It will inherit equals...
Which statement is NOT correct? The Virtual storage Access Method a. is used for very large files ..
Which statement is NOT correct? The Virtual storage Access Methoda. is used for very large files that need both direct access and batch processing.b. may use an overflow area for records.c. provides an exact physical address for each record.d. is appropriate for files that require few insertions or deletions. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT