Write a menu-driven java program (named FileManip) that will allow for the creat
ID: 3536018 • Letter: W
Question
Write a menu-driven java program (named FileManip) that will allow for the creation of and reading from a text file containing floating point numbers. The menu options hsould be
1. Create File
2. Read File
3. Exit Program
create File:- when this menu item is chosen it should create a file named floatnumber.txt. the program should then prompt the user to enter a series of positive floating point numbers . As the numbers are entered into the keyboard they should be written to the text file. The program should continue taking numbers from the keyboard and keep writing them to the file until the user wishes to finish.
Read File:- The program should read the floating point numbers from the floatnumbers.txt file, displaying each number to the screen and displaying a TOTAL of the numbers read. your program should only read from the file if it exists because the user might choose this menu option before menu item1.
Exit Program:- The program should terminate execution. Ensure that you write apropriate methods as a part of your solution which utilises appropriate parameter passing. Make sure that you use appropriate user prompts and labels for all output to the screen.
Explanation / Answer
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
public class ReadWriteTextFile {
private static void doReadWriteTextFile() {
try {
// input/output file names
String inputFileName = "README_InputFile.txt";
String outputFileName = "ReadWriteTextFile.out";
// Create FileReader Object
FileReader inputFileReader = new FileReader(inputFileName);
FileWriter outputFileReader = new FileWriter(outputFileName);
// Create Buffered/PrintWriter Objects
BufferedReader inputStream = new BufferedReader(inputFileReader);
PrintWriter outputStream = new PrintWriter(outputFileReader);
// Keep in mind that all of the above statements can be combined
// into the following:
//BufferedReader inputStream = new BufferedReader(new FileReader("README_InputFile.txt"));
//PrintWriter outputStream = new PrintWriter(new FileWriter("ReadWriteTextFile.out"));
outputStream.println("+---------- Testing output to a file ----------+");
outputStream.println();
String inLine = null;
while ((inLine = inputStream.readLine()) != null) {
outputStream.println(inLine);
}
outputStream.println();
outputStream.println("+---------- Testing output to a file ----------+");
outputStream.close();
inputStream.close();
} catch (IOException e) {
System.out.println("IOException:");
e.printStackTrace();
}
}
public static void main(String[] args) {
doReadWriteTextFile();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.