mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
Klasse FileLoader um Textdateien zu laden
This commit is contained in:
88
src/main/java/schule/ngb/zm/util/FileLoader.java
Normal file
88
src/main/java/schule/ngb/zm/util/FileLoader.java
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package schule.ngb.zm.util;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public final class FileLoader {
|
||||||
|
|
||||||
|
public static final Charset ASCII = StandardCharsets.US_ASCII;
|
||||||
|
|
||||||
|
public static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
public static final Charset UTF16 = StandardCharsets.UTF_16;
|
||||||
|
|
||||||
|
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
|
||||||
|
|
||||||
|
|
||||||
|
public static List<String> loadLines( String source ) {
|
||||||
|
return loadLines(source, UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> loadLines( String source, Charset charset ) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.EMPTY_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String loadText( String source ) {
|
||||||
|
return loadText(source, UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String loadText( String source, Charset charset ) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double[][] loadDoubles( String source, char separator, boolean skipFirst ) {
|
||||||
|
return loadDoubles(source, separator, skipFirst, UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double[][] loadDoubles( 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) -> Arrays
|
||||||
|
.stream(line.split(Character.toString(separator)))
|
||||||
|
.mapToDouble(
|
||||||
|
(value) -> {
|
||||||
|
try {
|
||||||
|
return Double.parseDouble(value);
|
||||||
|
} catch( NumberFormatException nfe ) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).toArray()
|
||||||
|
).toArray(double[][]::new);
|
||||||
|
} catch( IOException | URISyntaxException ex ) {
|
||||||
|
LOG.warn(ex, "Error while loading double values from csv source <%s>", source);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new double[0][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileLoader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Log LOG = Log.getLogger(FileLoader.class);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ import java.util.Objects;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class ImageLoader {
|
public final class ImageLoader {
|
||||||
|
|
||||||
public static boolean caching = true;
|
public static boolean caching = true;
|
||||||
|
|
||||||
|
|||||||
81
src/test/java/schule/ngb/zm/util/FileLoaderTest.java
Normal file
81
src/test/java/schule/ngb/zm/util/FileLoaderTest.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
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.*;
|
||||||
|
|
||||||
|
class FileLoaderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadLines() {
|
||||||
|
String[] data;
|
||||||
|
List<String> lines;
|
||||||
|
|
||||||
|
data = new String[]{
|
||||||
|
"Header1,Header2,Header3",
|
||||||
|
"1.1,1.2,1.3",
|
||||||
|
"2.1,2.2,2.3",
|
||||||
|
"3.1,3.2,3.3"
|
||||||
|
};
|
||||||
|
|
||||||
|
lines = FileLoader.loadLines("data_comma.csv");
|
||||||
|
assertEquals(data.length, lines.size());
|
||||||
|
for( int i = 0; i < lines.size(); i++ ) {
|
||||||
|
assertEquals(data[i], lines.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
};
|
||||||
|
|
||||||
|
lines = FileLoader.loadLines("data_semicolon_latin.csv", FileLoader.ISO_8859_1);
|
||||||
|
assertEquals(data.length, lines.size());
|
||||||
|
for( int i = 0; i < lines.size(); i++ ) {
|
||||||
|
assertEquals(data[i], lines.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadText() {
|
||||||
|
String data;
|
||||||
|
String text;
|
||||||
|
|
||||||
|
data = "Header1,Header2,Header3\n" +
|
||||||
|
"1.1,1.2,1.3\n" +
|
||||||
|
"2.1,2.2,2.3\n" +
|
||||||
|
"3.1,3.2,3.3\n";
|
||||||
|
text = FileLoader.loadText("data_comma.csv");
|
||||||
|
assertEquals(data, text);
|
||||||
|
|
||||||
|
data = "Nöme;Häder2;Straße\n" +
|
||||||
|
"1.1;1.2;1.3\n" +
|
||||||
|
"2.1;2.2;2.3\n" +
|
||||||
|
"3.1;3.2;3.3\n";
|
||||||
|
text = FileLoader.loadText("data_semicolon_latin.csv", FileLoader.ISO_8859_1);
|
||||||
|
assertEquals(data, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadCsv() {
|
||||||
|
double[][] data;
|
||||||
|
double[][] csv;
|
||||||
|
|
||||||
|
data = new double[][]{
|
||||||
|
{1.1,1.2,1.3},
|
||||||
|
{2.1,2.2,2.3},
|
||||||
|
{3.1,3.2,3.3}
|
||||||
|
};
|
||||||
|
csv = FileLoader.loadDoubles("data_comma.csv", ',', true);
|
||||||
|
assertArrayEquals(data, csv);
|
||||||
|
|
||||||
|
csv = FileLoader.loadDoubles("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1);
|
||||||
|
assertArrayEquals(data, csv);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
4
src/test/resources/data_comma.csv
Normal file
4
src/test/resources/data_comma.csv
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Header1,Header2,Header3
|
||||||
|
1.1,1.2,1.3
|
||||||
|
2.1,2.2,2.3
|
||||||
|
3.1,3.2,3.3
|
||||||
|
4
src/test/resources/data_semicolon.csv
Normal file
4
src/test/resources/data_semicolon.csv
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Header1;Header2;Header3
|
||||||
|
1.1;1.2;1.3
|
||||||
|
2.1;2.2;2.3
|
||||||
|
3.1;3.2;3.3
|
||||||
|
4
src/test/resources/data_semicolon_latin.csv
Normal file
4
src/test/resources/data_semicolon_latin.csv
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Nöme;Häder2;Straße
|
||||||
|
1.1;1.2;1.3
|
||||||
|
2.1;2.2;2.3
|
||||||
|
3.1;3.2;3.3
|
||||||
|
Reference in New Issue
Block a user