Simple java program that implements some basic file

Assignment Help JAVA Programming
Reference no: EM13272823

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

Reference no: EM13272823

Questions Cloud

Write a marketing plan : Identify an organization for which you would like to write a marketing plan. This can be an existing organization or a business that you would like to start.
Examples of preliminary crimes : Which of the following are examples of preliminary crimes?
Complete the scale : What is the z score for a score of 79 when the mean score of all persons who complete a depression scale is 89 and the standard deviation is 5? Is a person with a score of 79 more or less depressed than most other people who complete the scale?
Directly cause thread to stop executing-thread execution : Which cannot directly cause a thread to stop executing? What is the name of the method used to start a thread execution?
Simple java program that implements some basic file : 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 ..
Differentiate between marketing in a business industry : Differentiate between marketing in a business industry of your choice to marketing within the health care industry. Be sure to discuss the key roles, the impact of the external environment, etc. within each industry.
What are different types of foriegn excahnge transicition : What are the different types of foriegn excahnge transicition?
London circus vendors : We contract with London Circus Vendors, LLC to be our food vendor at our London rollout of our circus. London Circus Vendors, LLC (LCVLLC) agrees to provide all food and beverages, and we will get a 20% commission on all sales.
Job descriptions : Create an argument that describes why job descriptions are necessary in a health care organization. Provide support for your argument.

Reviews

Write a Review

JAVA Programming Questions & Answers

  The commission employee inherits

The Commission Employee inherits from the Employee class. A Commission Employee contains a commission rate and a sales amount variable which are used as part of the pay calculation. An explicit value constructor should be provided to set all 3 val..

  User session mgr - socket and thread programs

User Session Mgr - Socket and Thread Programs

  Generates a random number between 1 and 100

Write a program that generates a random number between 1 and 100 (inclusive) and then prompts the user to guess the number. The user should be given up to 10 chances to guess the number.

  Allows a user to select a favorite basketball team

Write an application that allows a user to select a favorite basketball team from a list box. Include at least five teams in the list, and display the chosen team in a text field after the user makes a selection. Save the file as JBasketball.java. :

  Write a graphical user interface class

Write a graphical user interface class called SentenceCounterGUI. It should be able to manipulate the sentence of words and have the behaviour label.

  Java program to ask user to enter favorite color

Write a Java program to ask the user to enter favorite color, a favorite food, favorite animal, and first name of a friend or relative.

  If there are 4 command line arguments

If there are 4 command line arguments (a b c filename.dat) then the engine should take a,b,c and store the resulting x1 and x2 in filename.dat working in silent mode (no use of screen)

  Create application uses two-dimensional arrays

Create an application which uses 2-dimensional arrays to keep track of 5 students' grades, with each student having 3 different classes.

  Write a method heading for each method

Write a method heading for each method, write preconditions and postconditions for each method.

  Determine java application on web and structure functions

Determine the Java application on Web and explain how program structure functions. Explain the application in as much detail as possible.

  Have to implement a search algorithm

Suppose you have to implement a search algorithm in a high programming language such as Java or C++. You are given a linked list that is not sorted and it's rather small, in the order of a few dozen elements.

  Java application that reads a date in numeric form

Designand write a java application that reads a date in numeric form from a set of three fields and displays it in English within a label. Use appropriate buttons. For Example:

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd