Skip to content

Commit 5142770

Browse files
timis1timis1
andauthored
JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) (eugenp#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 <[email protected]>
1 parent 88e2537 commit 5142770

33 files changed

+461
-369
lines changed

core-java-modules/core-java-io-apis-2/README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,5 @@ This module contains articles about core Java input/output(IO) APIs.
44

55
### Relevant Articles:
66
- [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute)
7-
- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input)
87
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
9-
- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer)
10-
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
11-
- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line)
12-
- [Storing Java Scanner Input in an Array](https://www.baeldung.com/java-store-scanner-input-in-array)
13-
- [How to Take Input as String With Spaces in Java Using Scanner?](https://www.baeldung.com/java-scanner-input-with-spaces)
14-
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
15-
- [What’s the difference between Scanner next() and nextLine() methods?](https://www.baeldung.com/java-scanner-next-vs-nextline)
16-
- [Handle NoSuchElementException When Reading a File Through Scanner](https://www.baeldung.com/java-scanner-nosuchelementexception-reading-file)
8+
- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty)

core-java-modules/core-java-io-apis-2/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@
9292
<version>7.1.0</version>
9393
<scope>test</scope>
9494
</dependency>
95-
<dependency>
96-
<groupId>org.testng</groupId>
97-
<artifactId>testng</artifactId>
98-
<version>7.5</version>
99-
<scope>compile</scope>
100-
</dependency>
10195
</dependencies>
10296
<build>
10397
<finalName>core-java-io-apis-2</finalName>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.emptyfile;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.NoSuchFileException;
12+
import java.nio.file.Path;
13+
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.io.TempDir;
16+
17+
class CheckFileIsEmptyUnitTest {
18+
@Test
19+
void whenTheFileIsEmpty_thenFileLengthIsZero(@TempDir Path tempDir) throws IOException {
20+
File emptyFile = tempDir.resolve("an-empty-file.txt")
21+
.toFile();
22+
emptyFile.createNewFile();
23+
assertTrue(emptyFile.exists());
24+
assertEquals(0, emptyFile.length());
25+
}
26+
27+
@Test
28+
void whenFileDoesNotExist_thenFileLengthIsZero(@TempDir Path tempDir) {
29+
File aNewFile = tempDir.resolve("a-new-file.txt")
30+
.toFile();
31+
assertFalse(aNewFile.exists());
32+
assertEquals(0, aNewFile.length());
33+
}
34+
35+
boolean isFileEmpty(File file) {
36+
if (!file.exists()) {
37+
throw new IllegalArgumentException("Cannot check the file length. The file is not found: " + file.getAbsolutePath());
38+
}
39+
return file.length() == 0;
40+
}
41+
42+
@Test
43+
void whenTheFileDoesNotExist_thenIsFilesEmptyThrowsException(@TempDir Path tempDir) {
44+
File aNewFile = tempDir.resolve("a-new-file.txt")
45+
.toFile();
46+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> isFileEmpty(aNewFile));
47+
assertEquals(ex.getMessage(), "Cannot check the file length. The file is not found: " + aNewFile.getAbsolutePath());
48+
}
49+
50+
@Test
51+
void whenTheFileIsEmpty_thenIsFilesEmptyReturnsTrue(@TempDir Path tempDir) throws IOException {
52+
File emptyFile = tempDir.resolve("an-empty-file.txt")
53+
.toFile();
54+
emptyFile.createNewFile();
55+
assertTrue(isFileEmpty(emptyFile));
56+
57+
}
58+
59+
@Test
60+
void whenTheFileIsEmpty_thenFilesSizeReturnsTrue(@TempDir Path tempDir) throws IOException {
61+
Path emptyFilePath = tempDir.resolve("an-empty-file.txt");
62+
Files.createFile(emptyFilePath);
63+
assertEquals(0, Files.size(emptyFilePath));
64+
}
65+
66+
@Test
67+
void whenTheFileDoesNotExist_thenFilesSizeThrowsException(@TempDir Path tempDir) {
68+
Path aNewFilePath = tempDir.resolve("a-new-file.txt");
69+
assertThrows(NoSuchFileException.class, () -> Files.size(aNewFilePath));
70+
}
71+
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
## Core Java IO APIs
2-
3-
This module contains articles about core Java input/output(IO) APIs.
4-
5-
### Relevant Articles:
6-
- [Read Date in Java Using Scanner](https://www.baeldung.com/java-scanner-read-date)
7-
- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty)

core-java-modules/core-java-io-apis-3/pom.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.

core-java-modules/core-java-io-apis/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ This module contains articles about core Java input/output(IO) APIs.
1010
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](https://www.baeldung.com/java-path)
1111
- [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter)
1212
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
13-
- [Java Scanner](https://www.baeldung.com/java-scanner)
14-
- [Scanner nextLine() Method](https://www.baeldung.com/java-scanner-nextline)
15-
- [Java Scanner hasNext() vs. hasNextLine()](https://www.baeldung.com/java-scanner-hasnext-vs-hasnextline)
13+
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
14+
- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line)
15+
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)

core-java-modules/core-java-io-apis/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<version>${lombok.version}</version>
3232
<scope>provided</scope>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.testng</groupId>
36+
<artifactId>testng</artifactId>
37+
<version>7.5</version>
38+
<scope>compile</scope>
39+
</dependency>
3440
</dependencies>
3541

3642
<build>

core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/multinput/MultiInputs.java renamed to core-java-modules/core-java-io-apis/src/main/java/com/baeldung/multinput/MultiInputs.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
package com.baeldung.multinput;
2-
3-
import java.util.InputMismatchException;
4-
import java.util.Scanner;
5-
6-
public class MultiInputs {
7-
public void UsingSpaceDelimiter(){
8-
Scanner scanner = new Scanner(System.in);
9-
System.out.print("Enter two numbers: ");
10-
int num1 = scanner.nextInt();
11-
int num2 = scanner.nextInt();
12-
System.out.println("You entered " + num1 + " and " + num2);
13-
14-
}
15-
public void UsingREDelimiter(){
16-
Scanner scanner = new Scanner(System.in);
17-
scanner.useDelimiter("[\\s,]+");
18-
System.out.print("Enter two numbers separated by a space or a comma: ");
19-
int num1 = scanner.nextInt();
20-
int num2 = scanner.nextInt();
21-
System.out.println("You entered " + num1 + " and " + num2);
22-
23-
}
24-
public void UsingCustomDelimiter(){
25-
Scanner scanner = new Scanner(System.in);
26-
scanner.useDelimiter(";");
27-
System.out.print("Enter two numbers separated by a semicolon: ");
28-
try { int num1 = scanner.nextInt();
29-
int num2 = scanner.nextInt();
30-
System.out.println("You entered " + num1 + " and " + num2); }
31-
catch (InputMismatchException e)
32-
{ System.out.println("Invalid input. Please enter two integers separated by a semicolon."); }
33-
34-
}
35-
}
36-
1+
package com.baeldung.multinput;
2+
3+
import java.util.InputMismatchException;
4+
import java.util.Scanner;
5+
6+
public class MultiInputs {
7+
public void UsingSpaceDelimiter(){
8+
Scanner scanner = new Scanner(System.in);
9+
System.out.print("Enter two numbers: ");
10+
int num1 = scanner.nextInt();
11+
int num2 = scanner.nextInt();
12+
System.out.println("You entered " + num1 + " and " + num2);
13+
14+
}
15+
public void UsingREDelimiter(){
16+
Scanner scanner = new Scanner(System.in);
17+
scanner.useDelimiter("[\\s,]+");
18+
System.out.print("Enter two numbers separated by a space or a comma: ");
19+
int num1 = scanner.nextInt();
20+
int num2 = scanner.nextInt();
21+
System.out.println("You entered " + num1 + " and " + num2);
22+
23+
}
24+
public void UsingCustomDelimiter(){
25+
Scanner scanner = new Scanner(System.in);
26+
scanner.useDelimiter(";");
27+
System.out.print("Enter two numbers separated by a semicolon: ");
28+
try { int num1 = scanner.nextInt();
29+
int num2 = scanner.nextInt();
30+
System.out.println("You entered " + num1 + " and " + num2); }
31+
catch (InputMismatchException e)
32+
{ System.out.println("Invalid input. Please enter two integers separated by a semicolon."); }
33+
34+
}
35+
}
36+

core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java renamed to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
package com.baeldung.bufferedreadervsfilereader;
2-
3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import java.io.BufferedReader;
6-
import java.io.File;
7-
import java.io.IOException;
8-
import java.io.InputStreamReader;
9-
import java.nio.charset.StandardCharsets;
10-
import java.nio.file.Files;
11-
import java.nio.file.Path;
12-
13-
import org.junit.jupiter.api.Test;
14-
15-
class BufferedReaderUnitTest {
16-
17-
@Test
18-
void whenReadingAFile_thenReadsLineByLine() {
19-
StringBuilder result = new StringBuilder();
20-
21-
final Path filePath = new File("src/test/resources/sampleText1.txt").toPath();
22-
try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) {
23-
String line;
24-
25-
while((line = br.readLine()) != null) {
26-
result.append(line);
27-
result.append('\n');
28-
}
29-
} catch (IOException e) {
30-
e.printStackTrace();
31-
}
32-
33-
assertEquals("first line\nsecond line\nthird line\n", result.toString());
34-
}
35-
36-
}
1+
package com.baeldung.bufferedreadervsfilereader;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.BufferedReader;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
class BufferedReaderUnitTest {
16+
17+
@Test
18+
void whenReadingAFile_thenReadsLineByLine() {
19+
StringBuilder result = new StringBuilder();
20+
21+
final Path filePath = new File("src/test/resources/sampleText1.txt").toPath();
22+
try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) {
23+
String line;
24+
25+
while((line = br.readLine()) != null) {
26+
result.append(line);
27+
result.append('\n');
28+
}
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
33+
assertEquals("first line\nsecond line\nthird line\n", result.toString());
34+
}
35+
36+
}

core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java renamed to core-java-modules/core-java-io-apis/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
package com.baeldung.bufferedreadervsfilereader;
2-
3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import java.io.FileReader;
6-
import java.io.IOException;
7-
8-
import org.junit.jupiter.api.Test;
9-
10-
class FileReaderUnitTest {
11-
12-
@Test
13-
void whenReadingAFile_thenReadsCharByChar() {
14-
StringBuilder result = new StringBuilder();
15-
16-
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
17-
int i = fr.read();
18-
19-
while(i != -1) {
20-
result.append((char)i);
21-
22-
i = fr.read();
23-
}
24-
} catch (IOException e) {
25-
e.printStackTrace();
26-
}
27-
28-
assertEquals("qwerty", result.toString());
29-
}
30-
}
1+
package com.baeldung.bufferedreadervsfilereader;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
class FileReaderUnitTest {
11+
12+
@Test
13+
void whenReadingAFile_thenReadsCharByChar() {
14+
StringBuilder result = new StringBuilder();
15+
16+
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
17+
int i = fr.read();
18+
19+
while(i != -1) {
20+
result.append((char)i);
21+
22+
i = fr.read();
23+
}
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
28+
assertEquals("qwerty", result.toString());
29+
}
30+
}

0 commit comments

Comments
 (0)