JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) (#14361)
* JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) * JAVA-22516 Adding back the file after fixing the conflicts --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
@@ -10,6 +10,6 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](https://www.baeldung.com/java-path)
|
||||
- [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter)
|
||||
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
|
||||
- [Java Scanner](https://www.baeldung.com/java-scanner)
|
||||
- [Scanner nextLine() Method](https://www.baeldung.com/java-scanner-nextline)
|
||||
- [Java Scanner hasNext() vs. hasNextLine()](https://www.baeldung.com/java-scanner-hasnext-vs-hasnextline)
|
||||
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
|
||||
- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line)
|
||||
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
|
||||
@@ -31,6 +31,12 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>7.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.multinput;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MultiInputs {
|
||||
public void UsingSpaceDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter two numbers: ");
|
||||
int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2);
|
||||
|
||||
}
|
||||
public void UsingREDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
scanner.useDelimiter("[\\s,]+");
|
||||
System.out.print("Enter two numbers separated by a space or a comma: ");
|
||||
int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2);
|
||||
|
||||
}
|
||||
public void UsingCustomDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
scanner.useDelimiter(";");
|
||||
System.out.print("Enter two numbers separated by a semicolon: ");
|
||||
try { int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2); }
|
||||
catch (InputMismatchException e)
|
||||
{ System.out.println("Invalid input. Please enter two integers separated by a semicolon."); }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.PropertyConfigurator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.Scanner;
|
||||
|
||||
@Log4j
|
||||
public class HasNextVsHasNextLineDemo {
|
||||
private static final String LINE = "----------------------------";
|
||||
private static final String END_LINE = "--------OUTPUT--END---------\n";
|
||||
|
||||
|
||||
private static final String INPUT = new StringBuilder()
|
||||
.append("magic\tproject\n")
|
||||
.append(" database: oracle\n")
|
||||
.append("dependencies:\n")
|
||||
.append("spring:foo:bar\n")
|
||||
.append("\n").toString();
|
||||
|
||||
private static void hasNextBasic() {
|
||||
printHeader("hasNext() Basic");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
log.info(scanner.next());
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void hasNextWithDelimiter() {
|
||||
printHeader("hasNext() with delimiter");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
String token = scanner.next();
|
||||
if ("dependencies:".equals(token)) {
|
||||
scanner.useDelimiter(":");
|
||||
}
|
||||
log.info(token);
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void hasNextWithDelimiterFixed() {
|
||||
printHeader("hasNext() with delimiter FIX");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
String token = scanner.next();
|
||||
if ("dependencies:".equals(token)) {
|
||||
scanner.useDelimiter(":|\\s+");
|
||||
}
|
||||
log.info(token);
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void addLineNumber() {
|
||||
printHeader("add line number by hasNextLine() ");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
int i = 0;
|
||||
while (scanner.hasNextLine()) {
|
||||
log.info(String.format("%d|%s", ++i, scanner.nextLine()));
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void printHeader(String title) {
|
||||
log.info(LINE);
|
||||
log.info(title);
|
||||
log.info(LINE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
setLogger();
|
||||
hasNextBasic();
|
||||
hasNextWithDelimiter();
|
||||
hasNextWithDelimiterFixed();
|
||||
addLineNumber();
|
||||
}
|
||||
|
||||
//overwrite the logger config
|
||||
private static void setLogger() throws IOException {
|
||||
InputStream is = HasNextVsHasNextLineDemo.class.getResourceAsStream("/scanner/log4j.properties");
|
||||
Properties props = new Properties();
|
||||
props.load(is);
|
||||
LogManager.resetConfiguration();
|
||||
PropertyConfigurator.configure(props);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class NextLineAfterNextMethods {
|
||||
|
||||
private static void produceSkippingNextLineMethod() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("Enter your age: ");
|
||||
int age = scanner.nextInt();
|
||||
System.out.print("Enter your first name: ");
|
||||
String firstName = scanner.nextLine(); // Skipped because it reads the remaining newline character
|
||||
System.out.println(age + ":" + firstName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void fixSkippingNextLineMethodV1() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("Enter your age: ");
|
||||
int age = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
System.out.print("Enter your first name: ");
|
||||
String firstName = scanner.nextLine();
|
||||
System.out.println(age + ":" + firstName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void fixSkippingNextLineMethodV2() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("Enter your age: ");
|
||||
int age = Integer.parseInt(scanner.nextLine());
|
||||
System.out.print("Enter your first name: ");
|
||||
String firstName = scanner.nextLine();
|
||||
System.out.println(age + ":" + firstName);
|
||||
}
|
||||
}
|
||||
|
||||
private static void fixSkippingNextLineMethodV3() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("Enter your age: ");
|
||||
int age = scanner.nextInt();
|
||||
scanner.skip("\r\n");
|
||||
System.out.print("Enter your first name: ");
|
||||
String firstName = scanner.nextLine();
|
||||
System.out.println(age + ":" + firstName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
produceSkippingNextLineMethod();
|
||||
fixSkippingNextLineMethodV1();
|
||||
fixSkippingNextLineMethodV2();
|
||||
fixSkippingNextLineMethodV3();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BufferedReaderUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReadingAFile_thenReadsLineByLine() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
final Path filePath = new File("src/test/resources/sampleText1.txt").toPath();
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
|
||||
while((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
result.append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("first line\nsecond line\nthird line\n", result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReadingAFile_thenReadsCharByChar() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
|
||||
int i = fr.read();
|
||||
|
||||
while(i != -1) {
|
||||
result.append((char)i);
|
||||
|
||||
i = fr.read();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("qwerty", result.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.multinput;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.InputMismatchException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class TestMultipleInputsUnitTest {
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingSpaceDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "10 20\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingSpaceDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingREDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "30, 40\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingREDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingCustomDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "50; 60\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingCustomDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInput_whenUsingSpaceDelimiter_thenExpectInputMismatchException() {
|
||||
String input = "abc\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
Assertions.assertThrows(InputMismatchException.class, mi::UsingSpaceDelimiter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.outputtofile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
class DualPrintStream extends PrintStream {
|
||||
private final PrintStream second;
|
||||
|
||||
public DualPrintStream(OutputStream main, PrintStream second) {
|
||||
super(main);
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
second.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
super.flush();
|
||||
second.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buf, int off, int len) {
|
||||
super.write(buf, off, len);
|
||||
second.write(buf, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
super.write(b);
|
||||
second.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
super.write(b);
|
||||
second.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsoleOutputToFileUnitTest {
|
||||
|
||||
// @formatter:off
|
||||
private final static List<String> OUTPUT_LINES = Lists.newArrayList(
|
||||
"I came",
|
||||
"I saw",
|
||||
"I conquered");
|
||||
// @formatter:on
|
||||
|
||||
@Test
|
||||
void whenReplacingSystemOutPrintStreamWithFileOutputStream_thenOutputsGoToFile(@TempDir Path tempDir) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
Path outputFilePath = tempDir.resolve("file-output.txt");
|
||||
PrintStream out = new PrintStream(Files.newOutputStream(outputFilePath), true);
|
||||
System.setOut(out);
|
||||
|
||||
OUTPUT_LINES.forEach(line -> System.out.println(line));
|
||||
assertTrue(outputFilePath.toFile()
|
||||
.exists(), "The file exists");
|
||||
assertLinesMatch(OUTPUT_LINES, Files.readAllLines(outputFilePath));
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingDualPrintStream_thenOutputsGoToConsoleAndFile(@TempDir Path tempDir) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
Path outputFilePath = tempDir.resolve("dual-output.txt");
|
||||
DualPrintStream dualOut = new DualPrintStream(Files.newOutputStream(outputFilePath), System.out);
|
||||
System.setOut(dualOut);
|
||||
|
||||
OUTPUT_LINES.forEach(line -> System.out.println(line));
|
||||
assertTrue(outputFilePath.toFile()
|
||||
.exists(), "The file exists");
|
||||
assertLinesMatch(OUTPUT_LINES, Files.readAllLines(outputFilePath));
|
||||
System.setOut(originalOut);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,200 +0,0 @@
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaScannerUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadFileWithScanner_thenCorrect() throws IOException {
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read.in"));
|
||||
|
||||
assertTrue(scanner.hasNext());
|
||||
assertEquals("Hello", scanner.next());
|
||||
assertEquals("world", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertInputStreamToString_thenConverted() throws IOException {
|
||||
final String expectedValue = "Hello world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.useDelimiter("\\A");
|
||||
|
||||
final String result = scanner.next();
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadUsingBufferedReader_thenCorrect() throws IOException {
|
||||
final String firstLine = "Hello world";
|
||||
final String secondLine = "Hi, John";
|
||||
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read_multiple.in"));
|
||||
|
||||
String result = reader.readLine();
|
||||
assertEquals(firstLine, result);
|
||||
|
||||
result = reader.readLine();
|
||||
assertEquals(secondLine, result);
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadUsingScanner_thenCorrect() throws IOException {
|
||||
final String firstLine = "Hello world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read_multiple.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
final String result = scanner.nextLine();
|
||||
assertEquals(firstLine, result);
|
||||
|
||||
scanner.useDelimiter(", ");
|
||||
assertEquals("Hi", scanner.next());
|
||||
assertEquals("John", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingInputFromConsole_thenCorrect() {
|
||||
final String input = "Hello";
|
||||
final InputStream stdin = System.in;
|
||||
System.setIn(new ByteArrayInputStream(input.getBytes()));
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
final String result = scanner.next();
|
||||
assertEquals(input, result);
|
||||
|
||||
System.setIn(stdin);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidateInputUsingScanner_thenValidated() throws IOException {
|
||||
final String input = "2000";
|
||||
final InputStream stdin = System.in;
|
||||
System.setIn(new ByteArrayInputStream(input.getBytes()));
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
final boolean isIntInput = scanner.hasNextInt();
|
||||
assertTrue(isIntInput);
|
||||
|
||||
System.setIn(stdin);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScanString_thenCorrect() throws IOException {
|
||||
final String input = "Hello 1 F 3.5";
|
||||
final Scanner scanner = new Scanner(input);
|
||||
scanner.useLocale(Locale.US);
|
||||
|
||||
assertEquals("Hello", scanner.next());
|
||||
assertEquals(1, scanner.nextInt());
|
||||
assertEquals(15, scanner.nextInt(16));
|
||||
assertEquals(3.5, scanner.nextDouble(), 0.00000001);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindPatternUsingScanner_thenFound() throws IOException {
|
||||
final String expectedValue = "world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
final String result = scanner.findInLine("wo..d");
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindPatternInHorizon_thenFound() throws IOException {
|
||||
final String expectedValue = "world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
String result = scanner.findWithinHorizon("wo..d", 5);
|
||||
assertNull(result);
|
||||
|
||||
result = scanner.findWithinHorizon("wo..d", 100);
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkipPatternUsingScanner_thenSkiped() throws IOException {
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
scanner.skip(".e.lo");
|
||||
|
||||
assertEquals("world", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangeScannerDelimiter_thenChanged() throws IOException {
|
||||
final String expectedValue = "Hello world";
|
||||
final String[] splited = expectedValue.split("o");
|
||||
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.useDelimiter("o");
|
||||
|
||||
assertEquals(splited[0], scanner.next());
|
||||
assertEquals(splited[1], scanner.next());
|
||||
assertEquals(splited[2], scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithScannerTwoDelimiters_thenCorrect() throws IOException {
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read_d.in"));
|
||||
scanner.useDelimiter(",|-");
|
||||
|
||||
assertEquals("John", scanner.next());
|
||||
assertEquals("Adam", scanner.next());
|
||||
assertEquals("Tom", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void givenClosingScanner_whenReading_thenThrowException() throws IOException {
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.next();
|
||||
scanner.close();
|
||||
|
||||
final Scanner scanner2 = new Scanner(inputStream);
|
||||
scanner2.next();
|
||||
scanner2.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.scannernextline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ScannerNextLineUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingLines_thenCorrect() {
|
||||
String input = "Scanner\nTest\n";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
assertEquals("Scanner", scanner.nextLine());
|
||||
assertEquals("Test", scanner.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingPartialLines_thenCorrect() {
|
||||
String input = "Scanner\n";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
scanner.useDelimiter("");
|
||||
scanner.next();
|
||||
assertEquals("canner", scanner.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void givenNoNewLine_whenReadingNextLine_thenThrowNoSuchElementException() {
|
||||
try (Scanner scanner = new Scanner("")) {
|
||||
String result = scanner.nextLine();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenScannerIsClosed_whenReadingNextLine_thenThrowIllegalStateException() {
|
||||
Scanner scanner = new Scanner("");
|
||||
scanner.close();
|
||||
String result = scanner.nextLine();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
Hello world
|
||||
@@ -1 +0,0 @@
|
||||
John,Adam-Tom
|
||||
@@ -1,2 +0,0 @@
|
||||
Hello world
|
||||
Hi, John
|
||||
Reference in New Issue
Block a user