From b2aeac4eb88dc7ebdf7cb7252c087032a32da0f2 Mon Sep 17 00:00:00 2001 From: RanjeetKaur17 Date: Sat, 4 Aug 2018 16:45:02 +0400 Subject: [PATCH] Moving file parser inside the core-java module. Adding Test cases for reading files. --- .../fileparser/BufferedReaderExample.java | 12 +----- .../baeldung/fileparser/FileConstants.java | 8 ++++ .../fileparser/FileReaderExample.java | 31 ++++++++++++++++ .../fileparser/FilesReadLinesExample.java | 18 +++++++++ .../fileparser/ScannerIntExample.java | 12 +----- .../fileparser/ScannerStringExample.java | 12 +----- .../fileparser/BufferedReaderUnitTest.java | 19 ++++++++++ .../fileparser/FileReaderUnitTest.java | 19 ++++++++++ .../fileparser/FilesReadAllLinesUnitTest.java | 19 ++++++++++ .../fileparser/ScannerIntUnitTest.java | 19 ++++++++++ .../fileparser/ScannerStringUnitTest.java | 19 ++++++++++ .../src/test/resources/sampleNumberFile.txt | 2 + .../src/test/resources/sampleTextFile.txt | 2 + .../fileparser/FileReaderExample.java | 37 ------------------- .../fileparser/FilesReadLineExample.java | 28 -------------- 15 files changed, 159 insertions(+), 98 deletions(-) rename {file-parser/src => core-java/src/main/java}/com/baeldung/fileparser/BufferedReaderExample.java (54%) create mode 100644 core-java/src/main/java/com/baeldung/fileparser/FileConstants.java create mode 100644 core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java create mode 100644 core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java rename {file-parser/src => core-java/src/main/java}/com/baeldung/fileparser/ScannerIntExample.java (53%) rename {file-parser/src => core-java/src/main/java}/com/baeldung/fileparser/ScannerStringExample.java (54%) create mode 100644 core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java create mode 100644 core-java/src/test/resources/sampleNumberFile.txt create mode 100644 core-java/src/test/resources/sampleTextFile.txt delete mode 100644 file-parser/src/com/baeldung/fileparser/FileReaderExample.java delete mode 100644 file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java diff --git a/file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java b/core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java similarity index 54% rename from file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java rename to core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java index a139ed80ab..45ff486a79 100644 --- a/file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java +++ b/core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java @@ -7,18 +7,8 @@ import java.util.ArrayList; public class BufferedReaderExample { - private static final String FILENAME = "src/resources/txt.txt"; + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { - public static void main(String[] args) { - try { - System.out.println(generateArrayListFromFile(FILENAME)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static ArrayList generateArrayListFromFile(String filename) throws IOException { - ArrayList result = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader(filename))) { diff --git a/core-java/src/main/java/com/baeldung/fileparser/FileConstants.java b/core-java/src/main/java/com/baeldung/fileparser/FileConstants.java new file mode 100644 index 0000000000..1d3cce79f2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/FileConstants.java @@ -0,0 +1,8 @@ +package com.baeldung.fileparser; + +public class FileConstants { + + protected static final String TEXT_FILENAME = "src/main/resources/sampleTextFile.txt"; + + protected static final String NNUMBER_FILENAME = "src/main/resources/sampleNumberFile.txt"; +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java b/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java new file mode 100644 index 0000000000..f9dd2a9ec5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java @@ -0,0 +1,31 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class FileReaderExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (FileReader f = new FileReader(filename)) { + StringBuffer sb = new StringBuffer(); + while (f.ready()) { + char c = (char) f.read(); + if (c == '\n') { + result.add(sb.toString()); + sb = new StringBuffer(); + } else { + sb.append(c); + } + } + if (sb.length() > 0) { + result.add(sb.toString()); + } + } + + return result; + } +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java b/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java new file mode 100644 index 0000000000..8e74f7d386 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java @@ -0,0 +1,18 @@ +package com.baeldung.fileparser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class FilesReadLinesExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + List result = Files.readAllLines(Paths.get(filename)); + + return (ArrayList) result; + } + +} diff --git a/file-parser/src/com/baeldung/fileparser/ScannerIntExample.java b/core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java similarity index 53% rename from file-parser/src/com/baeldung/fileparser/ScannerIntExample.java rename to core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java index aab7455c30..25d17af4ea 100644 --- a/file-parser/src/com/baeldung/fileparser/ScannerIntExample.java +++ b/core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java @@ -7,17 +7,7 @@ import java.util.Scanner; public class ScannerIntExample { - private static final String FILENAME = "src/resources/num.txt"; - - public static void main(String[] args) { - try { - System.out.println(generateArrayListFromFile(FILENAME)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static ArrayList generateArrayListFromFile(String filename) throws IOException { + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { ArrayList result = new ArrayList<>(); diff --git a/file-parser/src/com/baeldung/fileparser/ScannerStringExample.java b/core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java similarity index 54% rename from file-parser/src/com/baeldung/fileparser/ScannerStringExample.java rename to core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java index fc18b53609..ec213c9490 100644 --- a/file-parser/src/com/baeldung/fileparser/ScannerStringExample.java +++ b/core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java @@ -7,17 +7,7 @@ import java.util.Scanner; public class ScannerStringExample { - private static final String FILENAME = "src/resources/txt.txt"; - - public static void main(String[] args) { - try { - System.out.println(generateArrayListFromFile(FILENAME)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static ArrayList generateArrayListFromFile(String filename) throws IOException { + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { ArrayList result = new ArrayList<>(); diff --git a/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java new file mode 100644 index 0000000000..78f900d796 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class BufferedReaderUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = BufferedReaderExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java new file mode 100644 index 0000000000..a38e58d348 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class FileReaderUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = FileReaderExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java new file mode 100644 index 0000000000..c0b742fd47 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class FilesReadAllLinesUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = FilesReadLinesExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java new file mode 100644 index 0000000000..0a398ba7c6 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class ScannerIntUnitTest { + + protected static final String NUMBER_FILENAME = "src/test/resources/sampleNumberFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetIntArrayList() throws IOException { + List numbers = ScannerIntExample.generateArrayListFromFile(NUMBER_FILENAME); + assertTrue("File does not has 2 lines", numbers.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java new file mode 100644 index 0000000000..8f9b0a56c0 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class ScannerStringUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = ScannerStringExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/resources/sampleNumberFile.txt b/core-java/src/test/resources/sampleNumberFile.txt new file mode 100644 index 0000000000..7787faa3c1 --- /dev/null +++ b/core-java/src/test/resources/sampleNumberFile.txt @@ -0,0 +1,2 @@ +111 +222 \ No newline at end of file diff --git a/core-java/src/test/resources/sampleTextFile.txt b/core-java/src/test/resources/sampleTextFile.txt new file mode 100644 index 0000000000..75cb50aafa --- /dev/null +++ b/core-java/src/test/resources/sampleTextFile.txt @@ -0,0 +1,2 @@ +Hello +World \ No newline at end of file diff --git a/file-parser/src/com/baeldung/fileparser/FileReaderExample.java b/file-parser/src/com/baeldung/fileparser/FileReaderExample.java deleted file mode 100644 index 1ab98973c7..0000000000 --- a/file-parser/src/com/baeldung/fileparser/FileReaderExample.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.fileparser; - -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; - -public class FileReaderExample { - - private static final String FILENAME = "src/resources/txt.txt"; - - public static void main(String[] args) { - try { - System.out.println(generateArrayListFromFile(FILENAME)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static ArrayList generateArrayListFromFile(String filename) throws IOException { - - ArrayList result = new ArrayList<>(); - - try (FileReader f = new FileReader(filename)) { - - while (f.ready()) { - char c = (char) f.read(); - - if (c != ' ' && c != '\t' && c != '\n') { - result.add(c); - } - } - return result; - } - - } - -} diff --git a/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java b/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java deleted file mode 100644 index 7f94525c22..0000000000 --- a/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.fileparser; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; - -public class FilesReadLineExample { - - private static final String FILENAME = "src/resources/txt.txt"; - - public static void main(String[] args) { - try { - System.out.println(generateArrayListFromFile(FILENAME)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static ArrayList generateArrayListFromFile(String filename) throws IOException { - - List result = Files.readAllLines(Paths.get(filename)); - - return (ArrayList) result; - } - -}