WikiGalaxy

Personalize

Java Read Files: FileReader

Using FileReader:

FileReader is a convenient class for reading character files. It makes it easy to read the content of a file as a stream of characters.


import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {
    public static void main(String[] args) {
        try (FileReader reader = new FileReader("example.txt")) {
            int character;
            while ((character = reader.read()) != -1) {
                System.out.print((char) character);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Content of example.txt

Java Read Files: BufferedReader

Using BufferedReader:

BufferedReader provides buffering for Reader instances. It makes the reading of large files more efficient by reducing the number of I/O operations.


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Line by line content of example.txt

Java Read Files: Scanner

Using Scanner:

Scanner is a versatile class that can be used to read input from various sources including files. It is particularly useful for parsing primitive types and strings using regular expressions.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(new File("example.txt"))) {
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Line by line content of example.txt

Java Read Files: FileInputStream

Using FileInputStream:

FileInputStream is used for reading binary data from a file. It is suitable for reading raw byte streams such as image data.


import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("example.txt")) {
            int byteContent;
            while ((byteContent = fis.read()) != -1) {
                System.out.print((char) byteContent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Content of example.txt in bytes

Java Read Files: NIO Files

Using NIO Files:

Java NIO (New I/O) Files class provides several static methods for file operations, including reading all lines of a file into a List.


import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;

public class NIOFilesExample {
    public static void main(String[] args) {
        try {
            List lines = Files.readAllLines(Paths.get("example.txt"));
            lines.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Line by line content of example.txt using NIO

Java Read Files: InputStreamReader

Using InputStreamReader:

InputStreamReader bridges from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset.


import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;

public class InputStreamReaderExample {
    public static void main(String[] args) {
        try (InputStreamReader isr = new InputStreamReader(new FileInputStream("example.txt"), "UTF-8")) {
            int character;
            while ((character = isr.read()) != -1) {
                System.out.print((char) character);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Content of example.txt using InputStreamReader

Java Read Files: RandomAccessFile

Using RandomAccessFile:

RandomAccessFile is used for reading and writing to a random access file. It allows you to move the file pointer to any location within the file.


import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileExample {
    public static void main(String[] args) {
        try (RandomAccessFile raf = new RandomAccessFile("example.txt", "r")) {
            raf.seek(0);
            String line = raf.readLine();
            System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

First line of example.txt using RandomAccessFile

Java Read Files: FileChannel

Using FileChannel:

FileChannel is part of Java NIO and allows for reading, writing, mapping, and manipulating files. It can be used to transfer data directly between channels.


import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.io.IOException;

public class FileChannelExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("example.txt");
             FileChannel fileChannel = fis.getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (fileChannel.read(buffer) > 0) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Content of example.txt using FileChannel

Java Read Files: Stream API

Using Stream API:

The Stream API in Java 8 allows for functional-style operations on streams of elements, including reading files.


import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;

public class StreamAPIExample {
    public static void main(String[] args) {
        try {
            Files.lines(Paths.get("example.txt")).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Console Output:

Line by line content of example.txt using Stream API

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025