eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

EOF (End of File) means a condition when we’re reading a file and have reached the end of that file. Understanding EOF detection is essential because, in some applications, we may need to read configuration files, process data, or validate files. In Java, there are several ways we can detect EOF.

In this tutorial, we’ll explore several methods for EOF detection in Java.

2. Example Setup

However, before we continue, let’s first create a sample text file containing dummy data for testing:

@Test
@Order(0)
public void prepareFileForTest() {
    File file = new File(pathToFile);

    if (!file.exists()) {
        try {
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            writer.write(LOREM_IPSUM);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This method must be run first before the other methods because it ensures the existence of the test file. Therefore, we add the @Order(0) annotation.

3. Detect EOF Using FileInputStream

In the first approach, we’ll use FileInputStream, which is a subclass of InputStream.

There’s a read() method that works by reading data byte by byte so that it produces a value of -1 when it reaches the EOF.

Let’s read our test file to the end of the file and store the data in a ByteArrayOutputStream object:

String readWithFileInputStream(String pathFile) throws IOException {
    try (FileInputStream fis = new FileInputStream(pathFile);
        ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        int data;
        while ((data = fis.read()) != -1) {
            baos.write(data);
        }
        return baos.toString();
    }
}

Now let’s create a unit test and make sure the test passes:

@Test
@Order(1)
public void givenDummyText_whenReadWithFileInputStream_thenReturnText() {
    try {
        String actualText = eofDetection.readWithFileInputStream(pathToFile);
        assertEquals(LOREM_IPSUM, actualText);
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

The advantage of FileInputStream is in terms of efficiency – it’s very fast. Unfortunately, there’s no method to read text per line, so in the case of reading a text file, we must convert from bytes to characters.

So, this method is suitable for reading binary data and provides flexibility in byte-by-byte processing. However, it requires more data conversion code if we want to read text data in a structured format.

4. Detect EOF Using BufferedReader

BufferedReader is a class in the java.io package that’s used to read text from the input stream. The way BufferedReader works is by buffering or temporarily storing data in memory.

In BufferedReader, there’s a readline() method that reads the file line by line and returns a null value if it reaches EOF:

String readWithBufferedReader(String pathFile) throws IOException {
    try (FileInputStream fis = new FileInputStream(pathFile);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader reader = new BufferedReader(isr)) {
        StringBuilder actualContent = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            actualContent.append(line);
        }
        return actualContent.toString();
    }
}

Here, the contents of the file are read by the readLine() method line by line. Then, the results are stored in the actualContent variable until it produces a null value which indicates EOF.

Next, let’s do a test to ensure the accuracy of the results:

@Test
@Order(2)
public void givenDummyText_whenReadWithBufferedReader_thenReturnText() {
    try {
        String actualText = eofDetection.readWithBufferedReader(pathToFile);
        assertEquals(LOREM_IPSUM, actualText);
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

Since we have a readLine() method, this technique is great for reading text data in a structured format like CSV. However, it’s not suitable for reading binary data.

5. Detect EOF Using Scanner

Scanner is a class in the java.util package that can be used to read input with various types of data, such as text, integers, and others.

Scanner provides a hasNext() method to read the entire contents of the file until it produces a false value, which indicates EOF :

String readWithScanner(String pathFile) throws IOException{
    StringBuilder actualContent = new StringBuilder();
    File file = new File(pathFile);
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
    	String line = scanner.nextLine();
        actualContent.append(line);
    }
    return actualContent.toString();
}

We can observe how scanner reads the file, as long as hasNext() evaluates to true. This means we can retrieve String values from the scanner using the nextLine() method until hasNext() evaluates to false, indicating that we’ve reached the EOF.

Let’s test to make sure the method works correctly:

@Test
@Order(3)
public void givenDummyText_whenReadWithScanner_thenReturnText() {
    try {
        String actualText = eofDetection.readWithScanner(pathToFile);
        assertEquals(LOREM_IPSUM, actualText);
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

The advantage of this method is that it’s very flexible and can read various types of data easily, but it’s less than ideal for binary data. However, performance can be slightly slower than BufferedReader, and it isn’t suitable for reading binary data.

6. Detect EOF Using FileChannel and ByteBuffer

FileChannel and ByteBuffer are classes in Java NIO (New I/O) that are improvements to traditional I/O.

FileChannel functions are used for handling file input and output operations, while ByteBuffer is utilized to handle binary data in the form of a byte array efficiently.

For EOF detection, we’ll use these two classes – FileChannel to read the file and ByteBuffer to store the results. The approach we use is to read the buffer until it returns the value -1, which indicates the end of the file (EOF):

String readFileWithFileChannelAndByteBuffer(String pathFile) throws IOException {
    try (FileInputStream fis = new FileInputStream(pathFile);
        FileChannel channel = fis.getChannel()) {
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        while (channel.read(buffer) != -1) {
            buffer.flip();
            buffer.clear();
        }
        return StandardCharsets.UTF_8.decode(buffer).toString();
    }
}

This time, we don’t need to use StringBuilder because we can get the results of reading the file from the converted or decoded ByteBuffer object.

Let’s again test to ensure the method works:

@Test
@Order(4)
public void givenDummyText_whenReadWithFileChannelAndByteBuffer_thenReturnText() {
    try {
        String actualText = eofDetection.readFileWithFileChannelAndByteBuffer(pathToFile);
        assertEquals(LOREM_IPSUM, actualText);
    } catch (IOException e) {
        fail(e.getMessage());
    }
}

This method provides high performance when reading or writing data from or to files, is suitable for random access, and supports MappedByteBuffer. However, its usage is more intricate and demands meticulous buffer management.

It’s particularly well-suited for reading binary data and applications that necessitate random file access.

7. FileInputStream vs. BufferedReader vs. Scanner vs. FileChannel and ByteBuffer

The following table summarizes the comparison between the four approaches, each of which has advantages and disadvantages:

Feature FileInputStream BufferedReader Scanner FileChannel and ByteBuffer
Data Type Binary Structured text Structured text Binary
Performance Good Good Good Excellent
Flexibility High Medium High Low
Ease of use Low High High Low

8. Conclusion

In this article, we learned four ways of EOF detection in Java.

Each approach has its advantages and disadvantages. The right choice depends on the specific needs of our application, whether it involves reading structured text data or binary data, and how critical performance is in our use case.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)