Course – LS – All

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

>> CHECK OUT THE COURSE

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.

As always, the full source code is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.