38.3k views
3 votes
Using each of he following:

a. Byte stream.
b. Binary format
c. Text Format The programs must end when entered name is empty.
2. Give the corresponding code for each of the above method to read the information of the 3 nt student you have entered. You should use Fixed length record of input data with delimiter for each field in order to be able to restore the data. If fixed length record is used set the size for the name to be 30 character. And if variable length then use Colon ":?" as delimiter between fields. Attach the code for each method down here.
A. Write using byte stream
B. Write using binary
C. Write using text
D. Read the 3 rd student data using byte stream
E. Read the 3 rd student data using binary
F. Read the 3 rd student data using text

1 Answer

6 votes

Final answer:

To read and write student information using different formats, you can use byte stream, binary format, and text format.

Step-by-step explanation:

To read and write student information using different formats, you can use byte stream, binary format, and text format. Each method has its own code implementation. For example, using byte stream to write student information:

try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
dos.writeUTF(studentName);
dos.writeInt(studentAge);
dos.writeDouble(studentGpa);
}

And to read the 3rd student's data using text format:

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
for (int i = 1; i <= 3; i++) {
br.readLine(); // Skip two lines
String studentData = br.readLine();
if (i == 3) {
String[] fields = studentData.split(":?");
String studentName = fields[0];
int studentAge = Integer.parseInt(fields[1]);
double studentGpa = Double.parseDouble(fields[2]);
}
}
}

User Jiayang
by
8.7k points