Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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

}

}

}

 

Java, Programming

  • Category:- Java
  • Reference No.:- M91264772
  • Price:- $30

Guranteed 24 Hours Delivery, In Price:- $30

Have any Question?


Related Questions in Java

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As