Merge pull request #4951 from RanjeetKaur17/develop.0.2.0
Moving File Parser Samples to core-java-io
This commit is contained in:
commit
42d8fabe6f
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.fileparser;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class BufferedReaderExample {
|
||||||
|
|
||||||
|
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||||
|
|
||||||
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
||||||
|
|
||||||
|
while (br.ready()) {
|
||||||
|
result.add(br.readLine());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.fileparser;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ScannerIntExample {
|
||||||
|
|
||||||
|
protected static ArrayList<Integer> generateArrayListFromFile(String filename) throws IOException {
|
||||||
|
|
||||||
|
ArrayList<Integer> result = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Scanner s = new Scanner(new FileReader(filename))) {
|
||||||
|
|
||||||
|
while (s.hasNext()) {
|
||||||
|
result.add(s.nextInt());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.fileparser;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ScannerStringExample {
|
||||||
|
|
||||||
|
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||||
|
|
||||||
|
ArrayList<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Scanner s = new Scanner(new FileReader(filename))) {
|
||||||
|
|
||||||
|
while (s.hasNext()) {
|
||||||
|
result.add(s.nextLine());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
2
core-java-io/src/test/resources/sampleNumberFile.txt
Normal file
2
core-java-io/src/test/resources/sampleNumberFile.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
111
|
||||||
|
222
|
2
core-java-io/src/test/resources/sampleTextFile.txt
Normal file
2
core-java-io/src/test/resources/sampleTextFile.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Hello
|
||||||
|
World
|
Loading…
x
Reference in New Issue
Block a user