Answer:
See explaination
Explanation:
//ReadFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile
{
public static void main(String[] args)
{
int elementsCount=0;
//Create a File class object
File file=null;
Scanner fileScanner=null;
String fileName="sample.txt";
try
{
//Create an instance of File class
file=new File(fileName);
//create a scanner class object
fileScanner=new Scanner(file);
//read file elements until end of file
while (fileScanner.hasNext())
{
double value=fileScanner.nextInt();
elementsCount++;
}
//print smallest value
System.out.println(elementsCount+" data values are read");
}
//Catch the exception if file not found
catch (FileNotFoundException e)
{
System.out.println("File Not Found");
}
}
}
Sample output:
sample.txt
10
20
30
40
50
output:
5 data values are read