Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose: To read data from a file. To write data to a file. To process string da

ID: 3585030 • Letter: P

Question

Purpose: To read data from a file. To write data to a file. To process string data character by character. See the sample code for reading data from a file and writing data to a file. Part 1: Download the code for this lab from your Canvas page. Read the source code and its documentation. Run each of the two main methods. Part 2: Step 1: Open Notepad on your computer. Type in five lines of text. Save the file. Write down the exact location and name of the file. Do not forget the .txt when you write the name down. Notepad automatically saves your file with the .txt extension. Step 2: Create a Java project that has only one class, the driver. In main( ) write the code to read the data from step 1 and print each line of text to the screen. Step 3: Create a new Java project. This project has only class, the driver with main( ). In main( ) write the code to create a file for output. Enter seven lines of text to write to the output file. Once the code is written to save all the lines to the file, write the code to print a message to the screen stating that the output file was created. Run main( ), then check your computer to verify the data file was created and the contents are correct. Open the text file using Notepad to verify your data was saved as a text file. Step 4. Create a new Java project. This project has only one class, the driver with main( ). In main( ) open a file for input. Use the text file you created in Step 3 above. The main()method needs to accomplish the following tasks. You can declare and use variables in main( ) and read one physical line of data at a time. Read all the steps a-e below before writing any code. a) Count and print how many times the letter ‘e’ or ‘E’ appear in the file. b) Count and print how many vowels are in the data file. c) Count and print how many characters (including ‘blanks’, but not newlines) are in the file. d) count and print the number of times a non-space character is preceded by a space character. e) Print the contents of the file on the screen in all uppercase.

Explanation / Answer

1)

computerinfo.txt

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus

_________________

ReadFileAndOutputOnScreen.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class ReadFileAndOutputOnScreen {

public static void main(String[] args) {

//Declaring variables

String line;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = null;

try {

//Opening the file  

sc = new Scanner(new File("computerinfo.txt"));

//reading the contents from the file

while (sc.hasNext()) {

//Displaying the output

System.out.println(sc.nextLine());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

sc.close();

}

}

}

__________________

Output:

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus

_________________

2)

WriteContentToOutputFile.java

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class WriteContentToOutputFile {

public static void main(String[] args) {
String line;

//Opening the output file
File f = new File("aboutComputer.txt");
FileWriter fw = null;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

// creates the output file
try {
f.createNewFile();
// creates a FileWriter Object
fw = new FileWriter(f);

for (int i = 1; i <= 7; i++) {
System.out.print("Enter Line#" + (i) + ":");
line = sc.nextLine();
fw.write(line + " ");

}


fw.close();
System.out.println("** Output file was created **");
} catch (Exception e) {
e.printStackTrace();
}


}

}

____________________

Input:

Enter Line#1:A computer is a device that can be instructed to carry out an
Enter Line#2:arbitrary set of arithmetic or logical operations automatically.
Enter Line#3:The ability of computers to follow a sequence of operations
Enter Line#4:called a program make computers very flexible and useful.
Enter Line#5:Since ancient times simple manual devices like the abacus
Enter Line#6:aided people in doing calculations. Early in the Industrial
Enter Line#7:Revolution, some mechanical devices were built to automate
** Output file was created **

_________________

Output:

aboutComputer.txt

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate

_________I Will complete the third part also..thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote