Java file Reader
File Reader:
It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file.
Input Stream:
Java.io package contains the Input Stream class. This stream represents the set of bytes, It is an abstract class.
Abstract Class:
Abstract methods are present in this class. An abstract method has an abstract keyword, and these methods don’t have anybody.
The syntax for object creation In Input Stream:
InputStream object1 = new FileInputStream();
Note: To create an object, we use FileInputStream because InputStream is an abstract class.
File Reader class Extends Reader class.
Reader Class:
It is used to read characters from the input stream, It throws an IO Exception while reading the input.
Sub-Classes in Reader:
- Buffered Reader
- Input Stream Reader
- File Reader
- String Reader.
Constructors In File Reader Class:
- FileReader (File f)
- FileReader (FileDescripter fdes)
- FileReader (String fN)
FileReader (File f):
It creates or generates a File Reader object to read the characters or data from the file.
Syntax for Object Creation:
FileReader obj1 = new FileReader(File f);
FileReader (FileDescripter fdes):
It creates or generates a File Reader object to read data, it Provides a File descriptor to read data.
Syntax for Object Creation:
FileReader obj1 = new FileReader(FileDescripter fdes);
FileReader (String fN):
It creates or generates a File Reader object to read data, it Provides a file name to read data from the file.
Syntax for Object Creation:
FileReader obj1 = new FileReader(String fN);
Methods in FileReader Class:
- read ()
- read (char c[])
- read (char c[], int start , int length)
- close ()
- skip (long n)
read():
It is used to read single characters from the stream. It returns -1 if it reaches the end of the input. It represents whether the next character is present or not. The return type of the read() method is int(Integer). It throws IOException i.e, Input or Output exception when an error occurs with inputs and outputs.
Syntax:
public int read() throws IOException
Program:
import java.io.*;
class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader f=new FileReader("cse.txt");
int i;
while((i=f.read())!=-1)
{
System.out.print((char)i);
}
f.close();
}
}
Output:
javatpoint
Note: Here, In cse.txt file contains data as JavaTpoint then, by using the read() method we also get output as JavaTpoint.
read (char c[]):
It reads the characters in the form of an array, It has a return value, An IO Exception occurs when an input or output error occurs.
Syntax:
public int read( char c[]) throws IOException
Program:
import java.io.*;
class FileReaderExample {
public static void main(String args[])throws Exception{
try
{
FileReader f=new FileReader("cse-c.txt");
char c[] = new char[5];
f.read(c);
System.out.println(Arrays.toString(c);
f.close();
}
Catch (Exception e)
{
System.out.println(e);
}
}
OUTPUT:
[‘W’,’ E’,’ L’,’ C’,’ O’]
NOTE: Here, In cse-c. txt file contains WELCOME as data then it reads the data in the form of arrays and it gives output as above.
read (char c[], int start , int length):
It stores the reading characters in the array form, It returns the count of characters. An integer is the return type.
Syntax:
public int read(char[] c, int start, int length)
- c -It is the Destination buffer
- start – at which index it starts writing characters.
- length -.Several characters
Program:
import java.io.*;
class Example3{
public static void main(String[] s) throws IOException{
FileReader object2 = new FileReader(“Vishu.txt”);
char c[]=new char[10];
object2.read(c,0,4);
System . out . println(c);
object2.close();
}
catch (IOException E){
System. out. println(E);
}
OUTPUT:
JAVA
NOTE: Here, In Vishu. txt file contains the data Java Programming hence by using the above method we get output as JAVA.
It is used to close the character stream. The return type of the close() method is void. If the stream has been closed, performing methods like read() and other methods throw an IOException.
SYNTAX:
public void close()
Program:
import java.io.*;
class Example3{
public static void main(String[] s) throws IOException{
FileReader object2 = new FileReader(“Vishu.txt”);
char c[]=new char[10];
object2.read(c,0,4);
System . out . println(c);
object2.close();
}
catch (IOException E){
System. out. println(E);
}
OUTPUT:
JAVA
NOTE: Here, In Vishu. txt file contains the data Java Programming hence by using the above method we get output as JAVA.
skip (long s):
It returns the number of skipped characters from the stream. Long is the return type.
Syntax:
public long skip(long s)
Program:
import java.io.*;
class Example14
{
public static void main(String s[]) throws IOException
{
FileReader b = new FileReader (Vishu.txt);
System . out . print((char)b.read());
b.skip(3);
System . out . print((char)b.read());
}
}
OUTPUT:
W
O