From 0238c2b94878c2ead6bc1bb74d30e523c85e3963 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sun, 28 May 2023 11:06:59 +0200 Subject: [PATCH] BAEL-6465: How to handle NoSuchElementException when reading a file through a Scanner ? (#13999) --- .../ScannerNoSuchElementException.java | 58 +++++++++++++++++++ ...ScannerNoSuchElementExceptionUnitTest.java | 43 ++++++++++++++ .../src/test/resources/emptyFile.txt | 0 3 files changed, 101 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java create mode 100644 core-java-modules/core-java-io-apis-2/src/test/resources/emptyFile.txt diff --git a/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java new file mode 100644 index 0000000000..e868381dc2 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/scanner/ScannerNoSuchElementException.java @@ -0,0 +1,58 @@ +package com.baeldung.scanner; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.NoSuchElementException; +import java.util.Scanner; + +public class ScannerNoSuchElementException { + + public static String readFileV1(String pathname) throws IOException { + Path pathFile = Paths.get(pathname); + if (Files.notExists(pathFile)) { + return ""; + } + + try (Scanner scanner = new Scanner(pathFile)) { + return scanner.nextLine(); + } + } + + public static String readFileV2(String pathname) throws IOException { + Path pathFile = Paths.get(pathname); + if (Files.notExists(pathFile)) { + return ""; + } + + try (Scanner scanner = new Scanner(pathFile)) { + return scanner.hasNextLine() ? scanner.nextLine() : ""; + } + } + + public static String readFileV3(String pathname) throws IOException { + Path pathFile = Paths.get(pathname); + if (Files.notExists(pathFile) || Files.size(pathFile) == 0) { + return ""; + } + + try (Scanner scanner = new Scanner(pathFile)) { + return scanner.nextLine(); + } + } + + public static String readFileV4(String pathname) throws IOException { + Path pathFile = Paths.get(pathname); + if (Files.notExists(pathFile)) { + return ""; + } + + try (Scanner scanner = new Scanner(pathFile)) { + return scanner.nextLine(); + } catch (NoSuchElementException exception) { + return ""; + } + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java new file mode 100644 index 0000000000..6aa7d8f9d6 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/ScannerNoSuchElementExceptionUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.util.NoSuchElementException; + +import org.junit.jupiter.api.Test; + +class ScannerNoSuchElementExceptionUnitTest { + + @Test + void givenEmptyFile_whenUsingReadFileV1_thenThrowException() { + Exception exception = assertThrows(NoSuchElementException.class, () -> { + ScannerNoSuchElementException.readFileV1("src/test/resources/emptyFile.txt"); + }); + + assertEquals("No line found", exception.getMessage()); + } + + @Test + void givenEmptyFile_whenUsingReadFileV2_thenSuccess() throws IOException { + String emptyLine = ScannerNoSuchElementException.readFileV2("src/test/resources/emptyFile.txt"); + + assertEquals("", emptyLine); + } + + @Test + void givenEmptyFile_whenUsingReadFileV3_thenSuccess() throws IOException { + String emptyLine = ScannerNoSuchElementException.readFileV3("src/test/resources/emptyFile.txt"); + + assertEquals("", emptyLine); + } + + @Test + void givenEmptyFile_whenUsingReadFileV4_thenSuccess() throws IOException { + String emptyLine = ScannerNoSuchElementException.readFileV4("src/test/resources/emptyFile.txt"); + + assertEquals("", emptyLine); + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/resources/emptyFile.txt b/core-java-modules/core-java-io-apis-2/src/test/resources/emptyFile.txt new file mode 100644 index 0000000000..e69de29bb2