|
|
|
The methods in the Scanner class can also be used to read input data from a file
File myFile; // Define a "File" type variable to receive // the opened file myFile = new File("Path-name-of-the-file"); // Open the file |
The variable myFile contains information about the opened file
The variable myFile will be used in read operations
|
|
|
|
|
|
Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { read a word from the Scanner object; print the word; } |
(I have used the initialization form to define the variables myFile and in for brevity.)
import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file String x; // Variable to receive a string while ( in.hasNext() ) { x = in.next(); // Read a string (word) System.out.println(x); // Print string read } System.out.println("Done"); } } |
Additional programming code needed:
|
How to run the program:
|
|
|
Open the file "inp2" Construct a Scanner object using the opened file sum = 0; // Initial value ("clear the slate") as long as ( there is a double datum in the Scanner object ) { Read a floating point number from the Scanner object; Add the number to the sum; } Print sum; |
import java.io.*; import java.util.Scanner; public class File02 { public static void main(String[] args) throws IOException { File myFile = new File("inp2"); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file double x; // Variable to receive a floating point number double sum; // Running sum sum = 0.0; // Initialize ("clear the slate") while ( in.hasNextDouble() ) { x = in.nextDouble(); // Read a floating point number sum = sum + x; // Add to the running sum } System.out.println("Sum = " + sum); } } |
How to run the program:
|
|
|
|
Open the file "inp3" Construct a Scanner object using the opened file sum = 0; // Initial value ("clear the slate") N = 0 // # items added into sum as long as ( there is a double datum in the Scanner object ) { Read a floating point number from the Scanner object; Add the number to the sum; N++; } Print sum/N; // print average |
import java.io.*; import java.util.Scanner; public class File03 { public static void main(String[] args) throws IOException { File myFile = new File("inp2"); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file double x; // Variable to receive a floating point number double sum; // Running sum int N; // # items added **** New code sum = 0.0; // Initialize ("clear the slate") N = 0; // No items added **** New code while ( in.hasNextDouble() ) { x = in.nextDouble(); // Read a floating point number sum = sum + x; // Add to the running sum N++; // One more item added **** New code } System.out.println("Average = " + sum/N); } } |
How to run the program:
|
|