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

  Part a -code comprehensionusing the uncommented sample code

part a -code comprehensionusing the uncommented sample code has been provided in moodleanswer the following questions1.

  What is an abstract class and how does it differ from the

question 1 what is an abstract class and how does it differ from a regular class? question 2 why are abstract class

  String that contains at least five letters

Write an application that prompts the user for a String that contains at least five letters and at least five digits. Continuously reprompt the user until a valid String is entered. Display a message indicating whether the user was successful or did ..

  supply a student tester class that tests all methods

Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods getName( ), addQuiz(int score), getTotalScore( ), and getAverageScore ( ).

  Part-2write a program that will perform some of the basic

part-2write a program that will perform some of the basic tasks accomplished by a file integrity checker such as

  Draw a uml diagram of each of the shapes

Draw a UML diagram of each of the Shapes, Rectangle and Square classes using the code that has been provided. Complete this using the examples that have been provide in the lecture slides - Sample program is provided that creates a list of shapes ..

  Objectivesto gain experience with arrays to gain

objectivesto gain experience with arrays. to gain experience with generic algorithms.documentationexplain

  Tic-tac-toe game assignment

Assignment 1(Java): Tic-Tac-Toe Game (Assignment 1 is attached),  Use the concepts and scenario from Assignment 1 and continue with the tic-tac-toe game design and development.  Section 1: Java Program File, The following method, numPaths, is suppos..

  Write a recursive method to produce a pattern

Write a recursive method to produce a pattern of n lines of asterisks.

  Program produces 1000 integer ranging

Then your program produces 1000 integer ranging from 1to 10000 in the array and then sort them in ascending order and then print the result into data.txt file. (You may list integers separated by space or new line)

  Design a program that asks the user to enter a series of 20

Number Analysis Program: Design a program that asks the user to enter a series of 20 numbers

  Dijikstra for undirected graph using simple scheme

Dijikstra for undirected graph using simple scheme with array and fibonacci heap and compare the performance/results, preferably in java.

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