mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 09:56:33 +02:00
Refactorings FileLoader
This commit is contained in:
@@ -29,7 +29,7 @@ public final class FileLoader {
|
||||
try {
|
||||
return Files.readAllLines(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset);
|
||||
} catch( IOException | URISyntaxException ex ) {
|
||||
LOG.warn(ex, "Error while loading lines from source <%s>", source);
|
||||
LOG.error(ex, "Error while loading lines from source <%s>", source);
|
||||
}
|
||||
|
||||
return Collections.EMPTY_LIST;
|
||||
@@ -43,17 +43,37 @@ public final class FileLoader {
|
||||
try {
|
||||
return Files.readString(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset);
|
||||
} catch( IOException | URISyntaxException ex ) {
|
||||
LOG.warn(ex, "Error while loading text from source <%s>", source);
|
||||
LOG.error(ex, "Error while loading text from source <%s>", source);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static double[][] loadDoubles( String source, char separator, boolean skipFirst ) {
|
||||
return loadDoubles(source, separator, skipFirst, UTF8);
|
||||
public static String[][] loadCsv( String source, boolean skipFirst ) {
|
||||
return loadCsv(source, ',', skipFirst, UTF8);
|
||||
}
|
||||
|
||||
public static double[][] loadDoubles( String source, char separator, boolean skipFirst, Charset charset ) {
|
||||
public static String[][] loadCsv( String source, char separator, boolean skipFirst, Charset charset ) {
|
||||
try {
|
||||
int n = skipFirst ? 1 : 0;
|
||||
return Files
|
||||
.lines(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset)
|
||||
.skip(n)
|
||||
.map(
|
||||
( line ) -> line.split(Character.toString(separator))
|
||||
).toArray(String[][]::new);
|
||||
} catch( IOException | URISyntaxException ex ) {
|
||||
LOG.error(ex, "Error while loading csv source <%s>", source);
|
||||
}
|
||||
|
||||
return new String[0][0];
|
||||
}
|
||||
|
||||
public static double[][] loadValues( String source, char separator, boolean skipFirst ) {
|
||||
return loadValues(source, separator, skipFirst, UTF8);
|
||||
}
|
||||
|
||||
public static double[][] loadValues( String source, char separator, boolean skipFirst, Charset charset ) {
|
||||
try {
|
||||
int n = skipFirst ? 1 : 0;
|
||||
return Files
|
||||
@@ -73,7 +93,7 @@ public final class FileLoader {
|
||||
).toArray()
|
||||
).toArray(double[][]::new);
|
||||
} catch( IOException | URISyntaxException ex ) {
|
||||
LOG.warn(ex, "Error while loading double values from csv source <%s>", source);
|
||||
LOG.error(ex, "Error while loading double values from csv source <%s>", source);
|
||||
}
|
||||
|
||||
return new double[0][0];
|
||||
|
||||
@@ -5,6 +5,7 @@ import schule.ngb.zm.Zeichenmaschine;
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
/**
|
||||
@@ -149,11 +150,17 @@ public class ResourceStreamProvider {
|
||||
return new URL(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ver
|
||||
* @param source
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static OutputStream getOutputStream( String source ) throws IOException {
|
||||
Validator.requireNotNull(source, "Resource source may not be null");
|
||||
Validator.requireNotEmpty(source, "Resource source may not be empty.");
|
||||
|
||||
return getOutputStream(new File(source));
|
||||
URL url = getResourceURL(source);
|
||||
return getOutputStream(new File(url.getPath()));
|
||||
}
|
||||
|
||||
public static OutputStream getOutputStream( File file ) throws IOException {
|
||||
|
||||
@@ -2,7 +2,6 @@ package schule.ngb.zm.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -62,7 +61,7 @@ class FileLoaderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadCsv() {
|
||||
void loadValues() {
|
||||
double[][] data;
|
||||
double[][] csv;
|
||||
|
||||
@@ -71,10 +70,43 @@ class FileLoaderTest {
|
||||
{2.1,2.2,2.3},
|
||||
{3.1,3.2,3.3}
|
||||
};
|
||||
csv = FileLoader.loadDoubles("data_comma.csv", ',', true);
|
||||
csv = FileLoader.loadValues("data_comma.csv", ',', true);
|
||||
assertArrayEquals(data, csv);
|
||||
|
||||
csv = FileLoader.loadDoubles("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1);
|
||||
csv = FileLoader.loadValues("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1);
|
||||
assertArrayEquals(data, csv);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadCsv() {
|
||||
String[][] data;
|
||||
String[][] csv;
|
||||
|
||||
data = new String[][]{
|
||||
{"Header1","Header2","Header3"},
|
||||
{"1.1","1.2","1.3"},
|
||||
{"2.1","2.2","2.3"},
|
||||
{"3.1","3.2","3.3"}
|
||||
};
|
||||
csv = FileLoader.loadCsv("data_comma.csv", false);
|
||||
assertArrayEquals(data, csv);
|
||||
|
||||
data = new String[][]{
|
||||
{"1.1","1.2","1.3"},
|
||||
{"2.1","2.2","2.3"},
|
||||
{"3.1","3.2","3.3"}
|
||||
};
|
||||
csv = FileLoader.loadCsv("data_semicolon.csv", ';', true, FileLoader.UTF8);
|
||||
assertArrayEquals(data, csv);
|
||||
|
||||
data = new String[][]{
|
||||
{"Nöme","Häder2","Straße"},
|
||||
{"1.1","1.2","1.3"},
|
||||
{"2.1","2.2","2.3"},
|
||||
{"3.1","3.2","3.3"}
|
||||
};
|
||||
|
||||
csv = FileLoader.loadCsv("data_semicolon_latin.csv", ';', false, FileLoader.ISO_8859_1);
|
||||
assertArrayEquals(data, csv);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user