Write a java program to store four records

Assignment Help JAVA Programming
Reference no: EM13725393

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) { }

}

}

}

 

Reference no: EM13725393

Questions Cloud

Questions on cyber security : Question 1: Submit a list of five cybersecurity breach case studies. Question 2: For each breach, write a brief statement covering the following points: a\ When did the incident take place (dates)? b\ What went wrong? c\ What was the outcome?
Write a bill for my american government class : I have to write a bill for my American Government class. Once you've found an interesting and relevant topic on which to write your bill and have done the necessary research
Ethics as a component of organizational culture : Write: Discuss the ethical "rules" within the organization where you work. If you are unemployed, choose a previous place of employment for the purpose of discussion in this paper.
How to draw the forming limit diagram : How to draw the forming limit diagram
Write a java program to store four records : 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 sto..
Describe the organizational structure : Describe the organizational structure of your selected organization. Compare and contrast that structure with two different organizational structures.
Why does the term energy feature so prominently in west : Why does the term "energy" feature so prominently in West's Contested Plains? Explain how new uses of energy transformed Native American lives on the plains in the eighteenth century.
Describe and discuss the target audience : Describe and discuss the target audience of the resource you selected. Describe and discuss the specific resource you have chosen
Describe the benefits package : The HR Director has asked you to come in to discuss your benefit package. Tell me how you will prepare yourself for this meeting. What questions would you ask concerning benefits? Describe the benefits package you would try to negotiate for your..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd