Moving file parser inside the core-java module.

Adding Test cases for reading files.
This commit is contained in:
RanjeetKaur17 2018-08-04 16:45:02 +04:00
parent 72d592c8d1
commit b2aeac4eb8
15 changed files with 159 additions and 98 deletions

View File

@ -7,17 +7,7 @@ import java.util.ArrayList;
public class BufferedReaderExample { public class BufferedReaderExample {
private static final String FILENAME = "src/resources/txt.txt"; protected static ArrayList<String> 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<String> generateArrayListFromFile(String filename) throws IOException {
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();

View File

@ -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";
}

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
ArrayList<String> 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;
}
}

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
List<String> result = Files.readAllLines(Paths.get(filename));
return (ArrayList<String>) result;
}
}

View File

@ -7,17 +7,7 @@ import java.util.Scanner;
public class ScannerIntExample { public class ScannerIntExample {
private static final String FILENAME = "src/resources/num.txt"; protected static ArrayList<Integer> 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<Integer> generateArrayListFromFile(String filename) throws IOException {
ArrayList<Integer> result = new ArrayList<>(); ArrayList<Integer> result = new ArrayList<>();

View File

@ -7,17 +7,7 @@ import java.util.Scanner;
public class ScannerStringExample { public class ScannerStringExample {
private static final String FILENAME = "src/resources/txt.txt"; protected static ArrayList<String> 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<String> generateArrayListFromFile(String filename) throws IOException {
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();

View File

@ -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<String> lines = BufferedReaderExample.generateArrayListFromFile(TEXT_FILENAME);
assertTrue("File does not has 2 lines", lines.size() == 2);
}
}

View File

@ -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<String> lines = FileReaderExample.generateArrayListFromFile(TEXT_FILENAME);
assertTrue("File does not has 2 lines", lines.size() == 2);
}
}

View File

@ -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<String> lines = FilesReadLinesExample.generateArrayListFromFile(TEXT_FILENAME);
assertTrue("File does not has 2 lines", lines.size() == 2);
}
}

View File

@ -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<Integer> numbers = ScannerIntExample.generateArrayListFromFile(NUMBER_FILENAME);
assertTrue("File does not has 2 lines", numbers.size() == 2);
}
}

View File

@ -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<String> lines = ScannerStringExample.generateArrayListFromFile(TEXT_FILENAME);
assertTrue("File does not has 2 lines", lines.size() == 2);
}
}

View File

@ -0,0 +1,2 @@
111
222

View File

@ -0,0 +1,2 @@
Hello
World

View File

@ -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<Character> generateArrayListFromFile(String filename) throws IOException {
ArrayList<Character> 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;
}
}
}

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
List<String> result = Files.readAllLines(Paths.get(filename));
return (ArrayList<String>) result;
}
}