Question
What is this code doing? What does it mean by reading from a file? Is it suppose to print something when it runs? I'm so
What is this code doing? What does it mean by "reading from a file"? Is it suppose to print something when it runs? I'm so confused. Can someone please explain.
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
public class readFile
{
public static void main(String[] args) throws IOException
{
if (args.length != 1)
{
System.out.println("usage: java readFile fileName");
System.exit(-1);
}
File fr = new File (args [0] );
if (!fr.exists())
{
System.out.println("Error: file " + args[0] + " not found. ");
System.exit(0);
}
Scanner infile = new Scanner(fr);
while (infile.hasNextLine())
{
String line = infile.nextLine();
System.out.println(line);
}
infile.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