-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program_for_reading_data_from_file_Using_FileReader.java
50 lines (40 loc) · 1.75 KB
/
Program_for_reading_data_from_file_Using_FileReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
FileReader
------------
we can use FileReader to read charachter data from file
Constructors
================================================================================================================
FileReader fr = new FileReader(String filename);
FileReader fr = new FileReader(File f);
================================================================================================================
*/
import java.io.*;
class FileReaderImplementation
{
public static void main(String[] args) throws IOException
{
// first approach
//---------------------------------------------------------------------------------------------
FileReader fr = new FileReader("G:\\samplefile.txt");
// (or) FileReader fr = new FileReader(new File("G:\\samplefile.txt"));
char[] ch = new char[(int)(new File("G:\\samplefile.txt")).length()];
fr.read(ch);
for(char ch1 : ch)
{
System.out.println(ch1);
}
//---------------------------------------------------------------------------------------------------
//==================================================================================================
// second approach
//------------------------------------------------------------------------------------------------------
FileReader fr1 = new FileReader("G:\\samplefile.txt");
// (or) FileReader fr = new FileReader(new File("G:\\samplefile.txt"));
int i = fr1.read();
while(i!=-1)
{
System.out.println((char)i);
i = fr1.read();
}
//--------------------------------------------------------------------------------------------------------
}
}