[BAEL-19088] - Move articles out of core-java-io part 3
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +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 FilesReadLinesExample {
|
||||
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
List<String> result = Files.readAllLines(Paths.get(filename));
|
||||
|
||||
return (ArrayList<String>) result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package org.baeldung.java;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.LineIterator;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
@Ignore("need large file for testing")
|
||||
public class JavaIoUnitTest {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// tests - iterate lines in a file
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException {
|
||||
final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
|
||||
// final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
|
||||
|
||||
logMemory();
|
||||
Files.readLines(new File(path), Charsets.UTF_8);
|
||||
logMemory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsIo_whenIteratingAFileInMemory_thenCorrect() throws IOException {
|
||||
// final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
|
||||
final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
|
||||
|
||||
logMemory();
|
||||
FileUtils.readLines(new File(path));
|
||||
logMemory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenStreamingThroughAFile_thenCorrect() throws IOException {
|
||||
final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
|
||||
// final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
|
||||
|
||||
logMemory();
|
||||
|
||||
FileInputStream inputStream = null;
|
||||
Scanner sc = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(path);
|
||||
sc = new Scanner(inputStream, "UTF-8");
|
||||
while (sc.hasNextLine()) {
|
||||
final String line = sc.nextLine();
|
||||
// System.out.println(line);
|
||||
}
|
||||
// note that Scanner suppresses exceptions
|
||||
if (sc.ioException() != null) {
|
||||
throw sc.ioException();
|
||||
}
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (sc != null) {
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
||||
logMemory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingApacheIo_whenStreamingThroughAFile_thenCorrect() throws IOException {
|
||||
final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv";
|
||||
// final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv";
|
||||
|
||||
logMemory();
|
||||
|
||||
final LineIterator it = FileUtils.lineIterator(new File(path), "UTF-8");
|
||||
try {
|
||||
while (it.hasNext()) {
|
||||
final String line = it.nextLine();
|
||||
// do something with line
|
||||
}
|
||||
} finally {
|
||||
LineIterator.closeQuietly(it);
|
||||
}
|
||||
|
||||
logMemory();
|
||||
}
|
||||
|
||||
// utils
|
||||
|
||||
private final void logMemory() {
|
||||
logger.info("Max Memory: {} Mb", Runtime.getRuntime()
|
||||
.maxMemory() / 1048576);
|
||||
logger.info("Total Memory: {} Mb", Runtime.getRuntime()
|
||||
.totalMemory() / 1048576);
|
||||
logger.info("Free Memory: {} Mb", Runtime.getRuntime()
|
||||
.freeMemory() / 1048576);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
111
|
||||
222
|
||||
@@ -1,2 +0,0 @@
|
||||
Hello
|
||||
World
|
||||
Reference in New Issue
Block a user