BAEL-6465: How to handle NoSuchElementException when reading a file through a Scanner ? (#13999)
This commit is contained in:
parent
d9027f6a1b
commit
0238c2b948
|
@ -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 "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue