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

  How much more that person needs to spend to qualify

Program that takes the amount a person has spent and determines the discount ($15 per $175 spent).

  What value is assigned to the variable phrase

Create a class State with five properties to hold the information about a single state and a method that calculates the density (people per square mile) of the state.

  Write an application class that contains several methods

Write a driver class. This class should use the main method to call four methods in the application class that will display our JOptionPanes - Write an application class that contains several methods.

  Implement 4 sorting algorithms in a java "sort" class.

Implement 4 sorting algorithms in a Java " Sort " class. ( insertion sort, selection sort and quicksort, and mergesort). count the number of comparisons made.

  A method that takes a two-dimensional array

A method that takes a two-dimensional array of int's as a parameter and searches the array for the second parameter, returning true if the second parameter matches any of the integers in the array, and false otherwise.

  Make an explicit statement of the invariant of the set class

Write a new container class called ser, which is similar to a bag, except that a set can contain only one copy of any given item, You will need to change the interface a bit. For example, instead of the bag's count function, you'll want a constant..

  Show running versions of the program

Show three running versions of the program in a single screen shot with 3 different size stop signs - Make the circle a red filled circle. Add 4 buttons below the Left-Right-Up-Down buttons with Red and any three other colours.

  Create a 2-by-3 two-dimensional array of integers

Create a 2-by-3 two-dimensional array of integers and fill it with data. Loop through the array and locate the smallest value stored.

  Write java program to compute how much federal need to pay

Write a java application to calculate how much federal and state tax you need to pay. The program should accomplish the following task.

  Write java applet for costco gas station

Write a java applet for costco gas station. The applet will first ask you whether you are a costco customer, then the grade of gas you want to use: grade 87, 89, or 91.

  A jsp expression can contain any java expression

A JSP expression can contain any Java expression that evaluates to a

  Assume that you are developing a java program which uses

suppose that youre developing a java program that uses decimal numbers and youd like to control the formatting of your

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