-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFileParser.java
More file actions
43 lines (37 loc) · 1.23 KB
/
FileParser.java
File metadata and controls
43 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FileParser {
/**
*
* @param filePath inputFile
* @return list of strings parsed from flat file
* @throws IOException
*/
public static List<String> parseFlatFile(String filePath) throws IOException {
List<String> resultList = new ArrayList<String>();
try {
File input = new File(filePath);
InputStream inputStream = new FileInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
resultList = reader.lines().map(mapToString).collect(Collectors.toList());
reader.close();
} catch (Exception e) {
throw new IOException("unable to parse file " + filePath + "; " + e.getMessage());
}
return resultList;
}
private static Function<String, String> mapToString = (line) -> {
String items[] = line.split("\\r?\\n");
String item = items[0];
return item;
};
}