Read an existing Text file and display the contents of Text file in a console output.




import java.io.*;
import java.util.*;
public class FileRead {
public static void main(String[] args) throws IOException{
Scanner input= new Scanner(System.in);
System.out.print("Please enter the file name to Open : ");
String file_name = input.nextLine();
FileInputStream fstream = new FileInputStream(file_name);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br1.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
System.out.println("File red successfully.");
}
}

Output:
/*
Please enter the file name to Open : faizalps.txt
Hello World!!!!!!!!!!!!!!!
File red successfully.
*/



Post a Comment

Copyright © Rough Record. Designed by OddThemes