DEV Community

SILAMBARASAN A
SILAMBARASAN A

Posted on

File handling in java

in java, file handling means working with files like create, read, write, update, deleting them.

it helps to store program informations, if needed it can access after program executed also.

why file handling needed?

  • to store data permanently instead of keeping only on the memory.
  • to read and write files from/to for later use.
  • to share data between different program or system.

  • to organize and manage large data efficiently.

to support file handling, java provides The File class in the java.io Package.

file handling class File, FileReader, FileWriter, BufferedReader, BufferedWriter.

1. File class
File class is used to represent a file or directory path in java.

The File class can:

  • Create file and directories
  • check whether a file exists
  • Get file information(name, size, path, etc)
  • delete file and directories

note: File class does not contains read or write.

import java.io.File;

public class Practice {
    public static void main(String[] args) {
        File file = new File("sample.txt");
        file.createNewFile();
    }
}
Enter fullscreen mode Exit fullscreen mode

common File class methods :

  • createNewFile()
  • exists()
  • delete()
  • isFile()
  • getName()
  • getPath()
  • list()
  • getAbsolutePath()
  • getCanonicalPath()

getAbsolutePath() → Returns full path as it is.
getCanonicalPath() → Returns simplified real path by removing . and .. .

2. FileWriter Class

in java, filewriter is a stream class. it is used to write text data for file.

Why File Writer?

  • write data in text file
  • update existing file
  • create new file and write

Constructor
1. Write Mode

FileWriter fw = new FileWriter("sample.txt");
Enter fullscreen mode Exit fullscreen mode

Delete existing data and write new data.

2. Append Mode

FileWriter fw = new FileWriter("sample.txt", true);
Enter fullscreen mode Exit fullscreen mode
import java.io.FileWriter;
import java.io.IOException;

class Main {
    public static void main(String[] args) throws IOException {

        FileWriter fw = new FileWriter("sample.txt");

        fw.write("Hello Java");

        fw.close();

        System.out.println("Data Written");
    }
}
Enter fullscreen mode Exit fullscreen mode

old data will can't delete and new data will add on the end of the old data.

Methods
write(String s) => used to write string
write(int c) => write single character
flush() => buffers data send yo file
close() => close the file

3. BufferedWriter Class

in java , BufferedWriter first data store in buffer then one times write in file.

Advantage

  • Faster then FileWriter
  • good for write Large amount of data
  • is there newLine() method
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {

        BufferedWriter bw =
            new BufferedWriter(new FileWriter("sample.txt"));

        bw.write("Hello Java");
        bw.newLine();
        bw.write("Welcome");

        bw.close();

        System.out.println("Data Written");
    }
}
Enter fullscreen mode Exit fullscreen mode

4. FileReader Class

FileReader is a character stream class. it is used to read characters from the text file.

why FileReader?

  • read data from text file
  • read character by character
  • it is commonly used for read text files (.txt)

Constructor

FileReader fr = new FileReader("sample.txt");
Enter fullscreen mode Exit fullscreen mode

sample.txt file open in read mode.

important methods
read() => read one character
close() => file will close

import java.io.FileReader;

class Main {
    public static void main(String[] args) throws Exception {

        FileReader fr = new FileReader("sample.txt");

        int ch = fr.read();

        System.out.println((char) ch);

        fr.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

How read() Works

int ch = fr.read();
Enter fullscreen mode Exit fullscreen mode
  • if read find the character return that ASCII/Unicode value.
  • if finds the end, return -1.

5. BufferedReader Class

BufferedReader is used to read text fastly from the text file.

Why BufferedReader?

  • File read line by line.
  • speed better then FileReader.
  • better for read large file.
  • is there readLine() method.

Important Methods

  • read() => read one character
  • readline => read one line
  • close() => close the Stream
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {

        BufferedReader br =
            new BufferedReader(new FileReader("sample.txt"));

        String line=br.readLine();

        while (line!= null) {
            System.out.println(line);
        }

        br.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)