Question
Hey guys! I am getting a FileNotFoundException for the below code. I have tried inputting the file names in different ways: 1) with the extension
Hey guys! I am getting a FileNotFoundException for the below code. I have tried inputting the file names in different ways:
1) with the extension e.g. Text.txt TextOutput.txt 2) without the extension e.g. Text 3) with the directoru User/.../Text.txt
None have worked. Any advice?
Error java.io.FileNotFoundException
CODE import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner;
public class WaterMelon {
public static void main(String[] args) throws FileNotFoundException { // Prompting for the input and output file names
// a new object called console to read input from user Scanner console = new Scanner(System.in); // prompting the user for the input file System.out.print("Input file: "); // saving the input value into the variable inputFileName String inputFileName= console.next(); // prompting the user for the output file System.out.print("Output file: "); // saving the input value into the variable inputFileName String outputFileName= console.next();
// Constructing the Scanner and PrintWriter objects for reading // and writing // creating an object called inputFile to read the input file File inputFile = new File(inputFileName); // reading in the input file Scanner in = new Scanner(inputFile); // creating an object called out to write to the output file PrintWriter out = new PrintWriter(outputFileName); // an int counter variable to capture the order of the line number int lineNumber = 1;
// Reading the input and writing the output // while the in object of type Scanner for the input file // has a next line while (in.hasNextLine()) { // Reading the content of the next line into the String var line String line = in.nextLine(); out.println("/* "+ lineNumber +" */"+line); lineNumber++; }
// closing the File and PrintWriter (in and out objects) in.close(); out.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started