Review the concept of files from your text book


Managing Error Handling and Files

Before you attempt this section, review the concept of Files from your text book. Double-Click on the IDE (Eclipse or Java Beans) on your desktop as directed by your Instructor. Once you launch your IDE, close the WELCOME page. You are now ready to follow the instructions below:
We are going to use PROGFIVE to read data from a file and then write data to a file. Note that file is read to a BufferedReader. We also need to set the filename to a value. Before, you start your coding, you need to create a file with text and store it in documents in the virtual lab. Type the following in the text file using Notepad.
It is a wonder day at Ashford University. I am in this class to learn Java and more precisely, how to use file. If I am working with files, I have completed 4 previous programs. I am not sure if it has been fun but it has been interesting.

Save the file as myfile.txt
Open Eclipse as you have done in the past and start coding. Create a new project called PROGFIVE and within the project create a class called testfiles (remember right click on src). The code below has minor errors that you will have to fix on your own. The errors are intentional. Also, do not just copy this code, try to rewrite using classes and methods.
Start a Java project

import java.io.*; //needed to handle files

public class testFiles {

public static void main(String[] args) {

String fileName = "myfile.txt"; //you need to include the full file path in between " "

// This will reference one line at a time

String line = null;

//below is the error handling if the file does not exist

try {

System.out.println("place your name here");

FileReader fileReader = new FileReader(fileName);

BufferedReader bufferedReader = new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {

System.out.println(line)

}

bufferedReader.close(); //closing file after processing

}

//error if the file does not exits

catch(FileNotFoundException ex) {

System.out.println("Unable to open file ‘');

}

catch(IOException ex) {

System.out.println( "Error reading file ")

}

}

}

Now you are ready to run your code.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Review the concept of files from your text book
Reference No:- TGS02384228

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)