Adarsh 11
Adarsh 11
In Java,With the help of File Class ,we can work with files,this files
class is inside the java.io package.File class can be used by creating
an object of the class and then specifying the name of the file.
Create a File
Read from a File
Write to a File
Delete a File
//Creating a file
import java.io.File;
Import java.io.IOException;
class GFG {
public static void main(String[] args)
{
try{
class GFG {
public static void main(String[] args)
{
try{
import java.io.FileWriter;
import java.io.File;
System.out.println("Writing successful");
//close the file
fw.close();
}
}
File Reader
FileReader is useful to read data in the form of characters from a ‘text’ file.
The constructors of this class assume that the default character encoding and
the default byte-buffer size are appropriate. To specify these values yourself,
construct an InputStreamReader on a FileInputStream.
public int read () throws IOException – Reads a single character. This method will
block until a character is available, an I/O error occurs, or the end of the stream is reached.
public int read(char[] cbuff) throws IOException – Reads characters into an array. This
method will block until some input is available, an I/O error occurs, or the end of the
stream is reached.
public abstract int read(char[] buff, int off, int len) throws IOException –Reads
characters into a portion of an array. This method will block until some input is available,
an I/O error occurs, or the end of the stream is reached.
Parameters:
cbuf – Destination buffer
off – Offset at which to start storing characters
len – Maximum number of characters to read
public void close() throws IOException closes the reader.
public long skip(long n) throws IOException –Skips characters. This method will block
until some characters are available, an I/O error occurs, or the end of the stream is
reached.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadFile
{
public static void main(String[] args) throws IOException
{
// variable declaration
int ch;