Ask Java Expert


Home >> Java

You will be writing a simple Java program that implements some basic file I/O operations. For the first exercise, you will write code that takes the name of a text file as input from the command line, opens that file if it exists and reads the file one line at a time. The program will reverse each line as it is read from the file and print the reversed line from the file to the console. For the second exercise, you will modify your code so that instead of printing each line reversed, the code prints the entire file reversed. For the third exercise you will modify your code one more time so that the program writes the output to a file instead of to the console.

Exercise 1 Description

Your code will prompt the user to enter a file name. If this file does not exist the program will produce an error message and exit. Otherwise the program will open the file and read a line from the file, reverse the line, and then print the line to the console. The program should process the entire file and terminate properly when the end of the file is reached.

Exercise 1 Sample Output

This is a sample transcript of what your program should do, assuming that the file lab11aInput.txt file provided above is used. Text in bold is expected input from the user rather than output from the program.

Enter an input name: lab11aInput.txt

ykcowrebbaJ

sevot yhtils eht dna ,gillirb sawT'

;ebaw eht ni elbmig dna eryg diD

,sevogorob eht erew ysmim llA

.ebargtuo shtar emom eht dnA

 

!nos ym ,kcowrebbaJ eht eraweB"

!hctac taht swalc eht ,etib taht swaj ehT

nuhs dna ,drib bujbuJ eht eraweB

"!hctansrednaB suoimurf ehT

 

:dnah ni drows laprov sih koot eH

%u2014thguos eh eof emoxnam eht emit gnoL

,eert mutmuT eht yb eh detser oS

.thguoht ni elihwa doots dnA

 

,doots eh thguoht hsiffu ni sa dnA

,emalf fo seye htiw ,kcowrebbaJ ehT

,doow yeglut eht hguorht gnilffihw emaC

!emac ti sa delbrub dnA

 

hguorht dna hguorht dna !owt ,enO !owt ,enO

!kcans-rekcins tnew edalb laprov ehT

daeh sti htiw dna ,daed ti tfel eH

.kcab gnihpmulag tnew eH

 

?kcowrebbaJ eht nials uoht tsah dnA"

!yob hsimaeb ym ,smra ym ot emoC

"!yallaC !hoollaC !yad suojbarf O

.yoj sih ni deltrohc eH

 

sevot yhtils eht dna ,gillirb sawT'

;ebaw eht ni elbmig dna eryg diD

,sevogorob eht erew ysmim llA

.ebargtuo shtar emom eht dnA

 

.)2781( erehT dnuoF ecilA tahW dna ,ssalG-gnikooL eht hguorhT morf --

 Note that if a file name is provided that does not exist, the program should provide an error message and end rather than crashing:

Enter an input file name: NoSuchFile.txt

There was a problem reading from NoSuchFile.txt

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab11b.java in the same ClosedLab11 folder.

For this exercise, you should extend the code that you wrote in Exercise 1. Your code should reverse the entire file instead of each individual line. (HINT: Think about how you can store ALL of the Strings from the file as you read it, then print them in reverse order).

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Compare this to the output provided in Exercise 1 above - the last line of Exercise 1 is the first line below, and the first line of Exercise 1 is the last line below. The entire file has been reversed:

Enter an input file name: lab11aInput.txt

.)2781( erehT dnuoF ecilA tahW dna ,ssalG-gnikooL eht hguorhT morf --

 

.ebargtuo shtar emom eht dnA

,sevogorob eht erew ysmim llA

;ebaw eht ni elbmig dna eryg diD

sevot yhtils eht dna ,gillirb sawT'

 

.yoj sih ni deltrohc eH

"!yallaC !hoollaC !yad suojbarf O

!yob hsimaeb ym ,smra ym ot emoC

?kcowrebbaJ eht nials uoht tsah dnA"

 

.kcab gnihpmulag tnew eH

daeh sti htiw dna ,daed ti tfel eH

!kcans-rekcins tnew edalb laprov ehT

hguorht dna hguorht dna !owt ,enO !owt ,enO

 

!emac ti sa delbrub dnA

,doow yeglut eht hguorht gnilffihw emaC

,emalf fo seye htiw ,kcowrebbaJ ehT

,doots eh thguoht hsiffu ni sa dnA

 

.thguoht ni elihwa doots dnA

,eert mutmuT eht yb eh detser oS

%u2014thguos eh eof emoxnam eht emit gnoL

:dnah ni drows laprov sih koot eH

 

"!hctansrednaB suoimurf ehT

nuhs dna ,drib bujbuJ eht eraweB

!hctac taht swalc eht ,etib taht swaj ehT

!nos ym ,kcowrebbaJ eht eraweB"

 

.ebargtuo shtar emom eht dnA

,sevogorob eht erew ysmim llA

;ebaw eht ni elbmig dna eryg diD

sevot yhtils eht dna ,gillirb sawT'

 

ykcowrebbaJ

 

Exercise 3

Create a copy of your solution for Exercise 2 and name it Lab11c.java in the same ClosedLab11 folder.

For this exercise, you should extend the code that you wrote in Exercise 2. Now instead of printing the output to the console, the program should output the reversed file to a new file. Your program should prompt the user to enter separate file names for input and for output, should display an error if the user tries to use the same filename for both input and output, and should handle the cases where the program cannot write to the output file.

Exercise 3 Sample Output

This is a sample transcript of what your program should do. In addition to the actual interaction at the console, you should check to make sure that your output file gets created and has the proper contents. You can do this by selecting File -> Open File from your Eclipse window and then browsing to find the appropriate filename.

Enter an input file name: lab11aInput.txt

Enter an output file name: lab11aOutput.txt

If you provide the same name for an input file as for an output file, the program should display an error message

Enter an input file name: lab11aInput.txt

Enter an output file name: lab11aInput.txt

ERROR! Your input file and output file MUST be different.

If you provide a file name that is unwriteable (such as attempting to write the file to a drive that does not exist on your system), you should receive an appropriate error message:

Enter an input file name: lab11aInput.txt

Enter an output file name: q:\lab11aOutput.txt

Error writing to file q:\lab11aOutput.txt

Java, Programming

  • Category:- Java
  • Reference No.:- M9852166

Have any Question?


Related Questions in Java

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 ...

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 ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

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 ...

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 ...

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 ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 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