18 How to read the data from files using java.util.Scanner class in Java?

For Ex: In this program I am showing how the Scanner class is loading data from the file test.txt.

text.txt File Contain:



ScanDemo.java

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

class ScanDemo{

public static void main(String s[] ) throws FileNotFoundException{
FileInputStream fis= new FileInputStream("C:\\Users\\Atharv Agrawal\\Desktop\\test.txt");

Scanner scan = new Scanner(fis);

String s1=scan.nextLine();
String s2=scan.nextLine();

System.out.println(s1+s2);
}

}



Output: 


Previous
Next Post »