File tree Expand file tree Collapse file tree
Section23JavaIOStreams/DemoCodes Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ //This Java program reads a text file character by character using FileInputStream and prints its content to the console.
Original file line number Diff line number Diff line change 1+ import java .io .FileInputStream ;
2+ public class Demo1
3+ {
4+ public static void main (String args []){
5+ try
6+ {
7+ FileInputStream fin =new FileInputStream ("C://Users//somes//Downloads//NoteApp//JavaEvolution-Learning-Growing-Mastering\\ Section23JavaIOStreams\\ DemoCodes\\ TXT Files\\ Demo1.txt" );
8+ int i =0 ;
9+
10+ while ((i =fin .read ())!=-1 )
11+ {
12+ System .out .print ((char )i );
13+ }
14+
15+ /*
16+ Reads the file one byte at a time.
17+ fin.read() returns an int representing the byte value. It returns -1 when the end of the file (EOF) is reached.
18+ (char)i converts the byte into a character, and it prints it to the console.
19+ So, this part prints the entire content of the file.
20+ */
21+ fin .close ();
22+ }
23+ catch (Exception e )
24+ {
25+ System .out .println (e );
26+ }
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments