This program is a Binary file I/O example problem. This is a pretty beginner cla
ID: 3837932 • Letter: T
Question
This program is a Binary file I/O example problem. This is a pretty beginner class so we haven’t discussed any major topics. We are using abstract methods and exception handling to deal with this problem/ arrays and other basic java topics binary object files.I will include an example as reference to how much we have used. Essentially this is an excersie on Binary file I/O
In this project, you read and write numbers from and to a binary file, first using output and input streams, then using a random access file
Step 1: Using a DataOutputStream or an ObjectOutputStream (your choice), write 10 randomly-generated numbers between 1 and 10 onto a binary file, then close the file. (Delete the file if it already exists).
Open the file using an appropriate input stream type and read the numbers into an array of the appropriate data type.
Write a method which takes as a parameter an array of numbers of the data type you are using. This method should sort the array in ascending order using a Selection, Insertion, or Bubble sort algorithm, again your choice.
Invoke this method on the array of numbers (putting them in ascending order).
Once sorted, display the 10 numbers on the console in sorted order.
Step 2: Do the same thing using a random access file.
In this step, do not close, then reopen the file, just reset the file pointer after writing the 10 numbers.
Submit a listing of your program and a PrintScreen showing the output of both steps above.
EXAMPLE OF CODE NOT PART OF THE QUESTION FOR CONTEXT
Explanation / Answer
This is a Java file to write data into binary.dat:
package com.numberToBinaryAndBinaryToNumber;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class WriteAndWrite {
public static void main(String[] args) throws IOException {
writeNumbers();
//readNumbers();
}
private static void writeNumbers() throws IOException {
DataOutputStream output = new DataOutputStream(new FileOutputStream("binary.dat"));
Random randomGenerator = new Random();
for (int i = 0; i < 10; i++) {
//int j=(int) Math.random();
//int j=randomGenerator.nextInt(10);
output.writeInt(i);
System.out.println(i);
}
for (int i = 0; i < 10; i++) {
output.writeByte(i);
System.out.println(i);
}
for (double i = 0.0; i < 10.0; i++) {
output.writeDouble(i);
System.out.println(i);
}
String str1="abc";
output.writeUTF(str1);
System.out.println(str1);
String str2="pqr";
output.writeUTF(str2);
System.out.println(str2);
output.close();
}
}
This is a Java file to read data from binary.dat:
package com.numberToBinaryAndBinaryToNumber;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class BinaryReadDemo {
private static final String FILE_NAME = "binary.dat" ;
public static void main(String[] args) {
// Open a binary file named "binary.dat"
DataInputStream inputStream = null ;
try {
inputStream = new DataInputStream(new FileInputStream(FILE_NAME));
}
catch (IOException e) {
System.out.println("Couldn't open input stream -- aborting...") ;
System.exit(0) ;
}
try {
// Read out the ten integers, bytes, and doubles from the file
System.out.print("Reading ten integers ") ;
for (int i = 1 ; i <= 10 ; i++) {
System.out.print(inputStream.readInt() + " ") ;
}
System.out.print(" Reading ten bytes ") ;
for (int i = 1 ; i <= 10 ; i++) {
System.out.print(inputStream.readByte() + " ") ;
}
System.out.print(" Reading ten doubles ") ;
for (int i = 1 ; i <= 10 ; i++) {
System.out.print(inputStream.readDouble() + " ") ;
}
// Read two Strings from the file
System.out.print(" Reading two strings ") ;
for (int i = 1 ; i <= 2 ; i++) {
System.out.println(" " + inputStream.readUTF()) ;
}
// Close the file
inputStream.close() ;
}
catch (IOException e) {
System.out.println("Error reading from file -- aborting...") ;
System.exit(0) ;
}
}
}
Output:
Reading ten integers
0 1 2 3 4 5 6 7 8
Reading ten bytes
0 0 0 9 0 1 2 3 4 5
Reading ten doubles
1.2688028221087231E-279 5.299808824E-315 5.304989477E-315 5.307579804E-315 5.31017013E-315 5.311465295E-315 5.31276046E-315 5.31405562E-315 5.315350785E-315 5.315998367E-315
Reading two strings
abc
pqr
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.