Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package edu.capella.bsit.drinkmenufile; import java.io . File; import java.io . FileNotFoundException; import java.io . FileOutputStream; import java.io . IOException; import java.io . PrintWriter; import java.util.InputMismatchException; import

package edu.capella.bsit.drinkmenufile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class DrinkMenuFile {
public static void main(String[] args){
String menuData = getMenuData("menu.txt");
System.out.println(menuData);
File outputFile = new File("output.txt");
writeMenuText(outputFile, menuData);
}
public static String getMenuData(String fileName){
// Use StringBuilder to assemble multiline string efficiently
StringBuilder menuString = new StringBuilder();
try{
FileOutputScream fos = new FileOutputStream(file, true);
Scanner fileScanner = new Scanner(new File(fileName));
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
menuString.append(line).append("
");
}
fileScanner.close();
} catch(InputMismatchException e){
System.out.println("Error reading file:" + e.getMessage());
}
return menuString.toString();
}
// Method to write the formatted menu to a text file.
public static void writeMenuText(File outputFile, String menuText){
try{
PrintWriter writer = new PrintWriter(outputFile);
writer.print(menuText);
writer.close();
} catch (FileNotFoundException e){
System.out.println("File not Found: "+ e.getMessage());
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

How organized or ready for action on this issue is this public?

Answered: 1 week ago

Question

What does this public know about your organization?

Answered: 1 week ago

Question

What does this public expect from your organization?

Answered: 1 week ago