Write a Java program to create a text file named ‘File1.txt.’ Write your Index Number and batch
name in two separate lines in the above file. Read the file and display the content of the text file.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
try {
// the file to be opened for reading
FileInputStream fis = new FileInputStream("File1.txt");
Scanner sc = new Scanner(fis);
System.out.println("Index Number is: "+sc.nextLine());
System.out.println("Batch name is: "+sc.nextLine());
sc.close(); // closes the scanner
} catch (IOException e) {
e.printStackTrace();
}
}
}
Comments
Leave a comment