Moving file parser inside the core-java module.
Adding Test cases for reading files.
This commit is contained in:
parent
c0fb463b89
commit
840b0e1f9c
|
@ -7,18 +7,8 @@ import java.util.ArrayList;
|
|||
|
||||
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<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
|
@ -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";
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> generateArrayListFromFile(String filename) throws IOException {
|
||||
protected static ArrayList<Integer> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<Integer> result = new ArrayList<>();
|
||||
|
|
@ -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<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
111
|
||||
222
|
|
@ -0,0 +1,2 @@
|
|||
Hello
|
||||
World
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue