You will handle student records in a file named


You will handle student records in a file named "student_record". Each record in the file consists of student number of integer type and student name of 32 characters, so the size of each record is 36 bytes. You need to write a Java program to store the following four records into the file:

72 James

56 Mark

87 John

30 Phillip

44 Andrew

Then, the program sorts the records in the file using any simple sorting algorithm such as bubble sorting, with the first field - student number, and prints all records in the file after sorting. When the program sorts the student records, the program  should not read all records in memory at once. The program should move records in the file. For example, after the program reads the first two records, it may switch the records (because 72 > 56) and write them in the same position in the file.

You need to submit Java programs and screen shots that show how your programs work.

Manipulating Files in Java

The following sections are prepared to help you understand how to manipulate files in Java application programs.

1. File

• Basic idea to use files

o Open - in Java, create an object for the given file

o Read; write

o Close

• Sequential access

• Random access

o Just keep reading or writing

o Read or write any place in a file

2. Sequential access

2.1 Writing

import java.io.*;

classFileWriteStreamTest {

public static void main (String[] args) {

FileWriteStreamTest f = new FileWriteStreamTest();

f.writeMyFile();

}

voidwriteMyFile() {

DataOutputStream dos = null;

String record = null;

intrecCount = 0;

try {

File f = new File("mydata.txt");

if (!f.exists())

f.createNewFile();

FileOutputStreamfos = new FileOutputStream(f);

BufferedOutputStreambos = new BufferedOutputStream(fos);

dos = new DataOutputStream(bos);

dos.writeBytes("Test\n");

dos.writeBytes("Welcome\n");

dos.writeBytes("Operating System\n");

dos.writeBytes("File System\n");

} catch (IOException e) {

System.out.println("Uh oh, got an IOException error!" +

} finally {

// if the file opened okay, make sure we close it

if (dos != null) {

try { dos.close(); }

catch (IOExceptionioe) { }

}

import java.io.*;

classFileReadStreamTest {

public static void main (String[] args) {

FileReadStreamTest f = new FileReadStreamTest();

f.readMyFile();

voidreadMyFile() {

DataInputStream dis = null;

String record = null;

intrecCount = 0;

File f = new File("mydata.txt");

if (!f.exists()) {

System.out.println(f.getName() + " does not exist");

return;

}

FileInputStreamfis = new FileInputStream(f);

BufferedInputStreambis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

while ( (record=dis.readLine()) != null ) {

recCount++;

System.out.println(recCount + ": " + record);

}

} catch (IOException e) {

System.out.println("Uh oh, got an IOException error!" +

} finally {

// if the file opened okay, make sure we close it

if (dis != null) {

try { dis.close(); }

catch (IOExceptionioe) { }

}

classFileRandomAccessTest {

public static void main (String[] args) {

FileRandomAccessTest f = new FileRandomAccessTest();

f.readWriteMyFile();

voidreadWriteMyFile() {

RandomAccessFileraf = null;

String s = null;

File f = new File("mydata.txt");

if (!f.exists()) // check if the file exists

f.createNewFile(); // create a new file

raf = new RandomAccessFile(f, "rw"); // open a file for random

access with "r", "rw"

if (raf.length() > 7) { // the size of the file

raf.seek(7); // move the file pointer

System.out.println(raf.readLine()); // read a line fromthe file pointer

file pointer

s = raf.readLine();

System.out.println(s);

raf.seek(raf.getFilePointer() - s.length()); // get the

raf.writeBytes("Test RamdomAccessFile\n"); // write bytes

}

} catch (IOException e) {

System.out.println("Uh oh, got an IOException error!" +

e.getMessage());

} finally {

// if the file opened okay, make sure we close it

if (raf != null) {

try { raf.close(); } // close the file

catch (IOExceptionioe) { }

}

}

}

 

Solution Preview :

Prepared by a verified Expert
JAVA Programming: You will handle student records in a file named
Reference No:- TGS0978622

Now Priced at $35 (50% Discount)

Recommended (90%)

Rated (4.3/5)