question archive Why is the created text file (The RavensGreenEggs blank? The file was created on my desktop but doesn't have any text in it
Subject:Computer SciencePrice: Bought3
Why is the created text file (The RavensGreenEggs blank?
The file was created on my desktop but doesn't have any text in it.
package SuessPoe;
//import all io package classes
import java.io.*;
//importing scanner class
import java.util.Scanner;
public class SuessPoeMashup
{
// static variable greenEggsFileScan of type Scanner
private static Scanner greenEggsFileScan;
// static variable ravenFileScan of type Scanner
private static Scanner ravenFileScan;
// static variable output of type PrintWriter
private static PrintWriter outputFile;
public static void main(String[] args) {
// String path variables for GreenEggsAndHam.txt, Raven.txt,
// AND TheRavensGreenEggs.txt having names greenEggsFilePath, ravenFilePath, AND
// outputFilePath
// RESPECTIVELY
String greenEggsFilePath, ravenFilePath, outputFilePath;
// assigning file locations with escape sequences to all three string variables
greenEggsFilePath = "C:\Users\cbweb\Desktop\Suess Poe\GreenEggsAndHam.txt";
ravenFilePath ="C:\Users\cbweb\Desktop\Suess Poe\Raven.txt";
outputFilePath = "C:\Users\cbweb\Desktop\Suess Poe\TheRavensGreenEggs.txt";
// using try catch to show message if any exception arises
try {
// File object for greenEggsFilePath variable
File greenEggsFile = new File(greenEggsFilePath);
// File object for ravenFilePath variable
File ravenFile = new File(ravenFilePath);
// Scanner object instantiation with greenEggsFile parameter
greenEggsFileScan = new Scanner(greenEggsFile);
// Scanner object instantiation with ravenFile parameter
ravenFileScan = new Scanner(ravenFile);
// PrintWriter object instantiation with outputFilePath string variable
outputFile = new PrintWriter(outputFilePath);
// Prompt the user that the files are being read
System.out.println("Reading files");
// reading both files greenEggsFileScan and ravenFileScan within while loop
while (greenEggsFileScan.hasNextLine() && ravenFileScan.hasNextLine()) {
// 1. reading current line of greenEggsFileScan file
String greenEggsLine = greenEggsFileScan.nextLine();
// 2. reading current line of ravenFileScan file
String ravenLine = ravenFileScan.nextLine();
// 4. printing greenEggsLine variable to output file
outputFile.println(greenEggsLine);
// 5. printing ravenLine variable to output file
outputFile.println(ravenLine);
}
// closing all three files
closeFiles();
// giving prompt to user, writing process done
System.out.println("The Raven's Green Eggs has been written");
// closing try...catch statements
} catch (Exception e) {
System.out.println("Issue arised: " + e.getMessage());
}
}
/**
* method to close all the files
*/
private static void closeFiles() {
// closing greenEggsFileScan file
greenEggsFileScan.close();
// closing ravenFileScan file
ravenFileScan.close();
// closing PrintWriter output file
outputFile.close();
}
}