Simple-Java-Creating-File-IO-Objects

File Name: StudentExtFilelo

DESCRIPTION

This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment.

In this assignment, each input and output statement will be changed. Otherwise, the code will mostly remain unchanged.

DISCUSSION

Creating File IO Objects

For doing this assignment, you will create a BufferedReader object to input data from the text file line at a time. Also, you will create a PrintWriter object to write data to the file a String at a time.

Getting Student Count

It is suggested that you put student count as the first line of data in the input file. Different methods of determining the total number of students are described below.

Prompt the User for the Count

Prompt the user for entering the total number of students in the file using JOptionPane.showInputDialog.

Put the Count in the File

Put the total number of students as the first line of data in the input file. Input this value from the file and create a StudentExt array of that size. Then input and process student data from the file.

Read File Twice

Create a file io object encapsulating the input file. Input data from the file line by line using the file io object. Keep a count of the number of lines read. Issue a close on the file io object. Create an array of StudentExtreferences of size above. Create a file io object encapsulating the input file a second time. Input data from the file line at a time. Each line will contain one student data. For each line read, create a StudentExt object and save its reference in the array created above. After all data is read and StudentExt objects are created, issue a close on the file io object. This will result in closing the file.

Here are the steps in detail.

Create the BufferedReader object (encapsulating the input file).

Input the file line by line using the BufferReader object. Keep a count of the number of lines read.

Close the BufferReader object.

Create an array of StudentExt references of size equal to count determined above.

Create the BufferedReader object (encapsulating the input file) a second time. (This will put the read pointer at the start of the file).

Read the file one line at a time. Each line contains one student data.

Create a StudentExt object according to student data read.

Save its reference in the array above.

When all student data is read, close the BufferedReader object.

Creating Text IO Files

It is suggested that you use JBuilder for creating the in.txt file.

A number of methods for creating in.txt file are presented below:

Using JBuilder

Adding in.txt File to Project

Right Click on the Project icon (.jpx file) in the left pane (Navigation Pane).

Select: Add package/file

Browse to the project directory (the directory where your .jpx file is stored).

Type the name of the file in the text field: in.txt

Click Ok.

When warned that file does not exist and that if you would like to create it, accept it by clicking ok.

The file is created. Its name will show up in the left pane (Navigation Pane).

Double click the file icon on the left pane. The empty file will show in the right pane (Content Pane).

The location of the file will show at the top of JBuilder Window. Make sure it is correct.

Type the contents of the file.

Adding out.txt File to Project

In your program, when you output to a file (say out.txt), the file gets automatically created and the output is added to the file. To see the contents of the file from JBuilder, add this file to your project as below:

Right Click on the Project icon (.jpx file) in the left pane (Navigation Pane).

Select: Add package/file

Browse to the project directory (the directory where your .jpx file is stored).

Select the file type to be “All Files”

Select an existing output file (say out.txt).

Click Ok.

The selected file be added to the project and its name will show up in the left pane (Navigation Pane).

Double click the file name, JBuilder will display the contents of the file.

Using MS Word

Enter the contents of the file in a blank file.

Name the file ending with .txt

Save the file as “Plain Text only” file.

Copy the file to the same directory where you project file (i.e. .jpx file) is.

Using NotePad or WordPad

The method is similar to MSWord.

Remember to enter the file name in quotation marks when saving the file in these editors.

For example, saving a file, infile.txt, enter the name as “infile.txt”.

If you do not put quotation marks around the name in these editors, often they put an additional .txt at the end of the file name.

SAMPLE CODE

Modifying Method Header

To make File IO work, add the phrase: throws Exception at the end of the method that calls file IO methods. For example, if File IO is done in the main method, write the main method header as below:

public static void main (String [] args) throws Exception

Inputting/Outputting Student Data To Files

//The sample code below input student data from file “in.txt”

//and output student data in file “out.txt”

import java.io.*;

import javax.swing.*;

public class TestStudentExt

{

public static void main(String[] args) throws Exception

{

String in,outAll, line;

int studentCount;

//Create a BufferedReader object for inputting froma file in.txt

BufferedReader br = new BufferedReader(new FileReader(“in.txt”));

//Create a PrintWriter object for outputting to a file out.txt.

PrintWriter pw = new PrintWriter (new FileWriter(“out.txt”));

//input the first line of the file containing the number of students

in = br.readLine();

studentCount = Integer.parseInt(in);

//Set up a for loop to input one student data during each pass through loop.

out = “Student report:n”;

for (int i=0; i<studentCount; i++)

{

//read one line containing one student data.

in = br.readLine();

//Create a StringTokenizer object to tokenize one student data.

//create the corresponding StudentExt object

}

//Set up a for loop to find grade of one student during each iteration

//Store the student data and the grade in a corresponding output String.

for (int i=0; i<studentCount; i++)

{

//Find one student grade

//Depending on student grade, accumulate its output in a corresponding String.

}

//Catenate all student output in a single String (say outAll)

//Output outAll String using using PrintWriter object.

//make sure to also call flush( ) after calling println()

pw.println(outAll);

pw.flush();

//Call close on File IO objects.

if (br != null)

br.close();

if (pw != null)

pw.close();

}

}

Sample Code For CopyFile Program

You don’t need to use this code for doing this assignment. This is presented here merely as a usage example.

The code below reads from one text file and writes it to another text file line at a time.

//Create a BufferedReader object for reading lines

// from the text file in.txt located in current directory.

BufferedReader br = new BufferedReader ( new FileReader( “in.txt” ) );

//create a PrinterWriter object for writing lines

//to the text file out.txt located in current directory.

PrintWriter pw =new PrintWriter ( new FileWriter( “out.txt” ) );

//Set up a read/write loop

//In this loop, you will read a line from the file

//using BufferedReader object

//and write the same line using the PrintWriter object.

//read a line (If there is no line left, readLine will return a null,

//other wise it will return a String containing the line).

line = br.readLine();

while (line != null)

{

//Write the line to the output file.

//Flush the line to send it to the file immediately.

//Otherwise, it will go to the output buffer

//and will go to the file when you issue close.

pw.println(line);

pw.flush();

//read the next line.

line = br.readLine();

}

//close the files.

//Closing the PrintWriter object will also cause a flush.

//It will write to the file any lines that may still

//be sitting in the output buffer.

if (br != null)

br.close();

if (pw != null)

pw.close();

}

TESTING

Input

The in.txt file should contain the following:

1, John Adam, 3, 93, 91, 100, Letter

2, Raymond Woo, 3, 65, 68, 63, Letter

3, Rick Smith, 3, 50, 58, 53, Letter

4, Ray Bartlett, 3, 62, 64, 69, Letter

5, Mary Russell, 3, 93, 90, 98, Letter

6, Andy Wong, 3, 89,88,84, Letter

7, Jay Russell, 3, 71,73,78, Letter

8, Jimmie Wong, 3, 70,77,72, Letter

9, Jackie Chan, 3, 85,89,84, Letter

10, Susan Wu, 3, 80,88,84, Letter

11, Bruce Lee, 4, 74, 79, 72, 75, Credit

12, Chuck Norris, 5, 63, 64, 62, 60, 68, Credit

13, Jet Li, 3, 85, 83, 89, Credit

14, Jessica Lauser, 3, 82, 84, 87, Letter

15, Mahnoosh Nik-Ahd, 2, 98, 99, Letter

In the above data, for the first entry, id is 1, name is John Adam, exam scores are 93, 91, 100 and grade type is letter grade.

Output

After running the program, out.txt file should contain the following:

1 John Adam (A)

5 Mary Russell (A)

15 Mahnoosh Nik-Ahd (A)

6 Andy Wong (B)

9 Jackie Chan (B)

10 Susan Wu (B)

14 Jessica Lauser (B)

7 Jay Russell (C)

8 Jimmie Wong (C)

2 Raymond Woo (D)

4 Ray Bartlett (D)

3 Rick Smith (F)

11 Bruce Lee (CR)

13 Jet Li (CR)

12 Chuck Norris (NCR)

SUBMIT

Turn in contents of in.txt and out.txt file in addition to source files.