Question: File I/O and Strings: First, examine the two programs ReadData and WriteData get a full understanding on how data can be read from a file,

File I/O and Strings:

First, examine the two programs ReadData and WriteData get a full understanding on how data can be read from a file, processed inside the program, and then written to a different file.

package readdata;

import java.util.Scanner;

public class ReadData { public static void main(String[] args) throws Exception { // Create a File instance java.io.File file = new java.io.File("scores.txt");

// Create a Scanner for the file Scanner input = new Scanner(file);

// Read data from a file while (input.hasNext()) { String firstName = input.next(); String mi = input.next(); String lastName = input.next(); int score = input.nextInt(); System.out.println( firstName + " " + mi + " " + lastName + " " + score); }

// Close the file input.close(); } }

package writedata;

public class WriteData { public static void main(String[] args) throws Exception { java.io.File file = new java.io.File("scores.txt"); if (file.exists()) { System.out.println("File already exists"); System.exit(0); }

// Create a file java.io.PrintWriter output = new java.io.PrintWriter(file);

// Write formatted output to the file output.print("John T Smith "); output.println(90); output.print("Eric K Jones "); output.println(85);

// Close the file output.close(); } }

Write a program that reads an input file of text, and writes the following to the output file:

name of the input file

the number of characters

the number of lines

whether the string java (upper or lower case characters) is contained in the input file.

The input and output file names should be passed as command line arguments to the program. In Netbeans, you can set the arguments by clicking on Run Set Project Configuration Customize, and enter the arguments. The two arguments are:

Lab12IP.txt for the input file name

Lab12OP.txt for the output file name

The program should:

Check to see if there are two arguments entered, and if not, send an error message to the console and exit.

(Hint: You can test this using args.length != 2.)

Check to see if the input file exists, and if not, send an error message to the console and then System.exit(0).

(Hint:You can test this using !inputFile.exists()

Using a Scanner object and while loops, you can read each line from the input file using the hasNext and nextLine methods. The lengths of all lines added together will give you the overall number of characters in the input file.

To test for the JAVA string, you can flip each line to upper case using the toUppercase method and use a Boolean to test to see if the line contains JAVA. You can use either the contains method in the String class to see if JAVA exists, or see if the index (e.g. indexOf method) is greater than or equal to 0.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!