im working on a java program that read a file with multiple lines , these lines has structure like this :
datadatadatadatadatadatadatadata "X" DATAKEY
datadatadatadatadatadatadatadata F DATA
datadatadatadatadatadatadatadata F DATA
datadatadatadatadatadatadatadata F DATA
datadatadatadatadatadatadatadata GDATA
datadatadatadatadatadatadatadata GDATA
datadatadatadatadatadatadatadata GDATA
datadatadatadatadatadatadatadata G9 0000
datadatadatadatadatadatadatadata "Z" KEY2
datadatadatadatadatadatadatadata "X" DATAKEY
datadatadatadatadatadatadatadata L 8888
datadatadatadatadatadatadatadata L 5678444
datadatadatadatadatadatadatadata L 7777
datadatadatadatadatadatadatadata L114100000
datadatadatadatadatadatadatadata L11410444
datadatadatadatadatadatadatadata L9 0000
datadatadatadatadatadatadatadata "Z" KEY2
datadatadatadatadatadatadatadata "X" DATAKEY
datadatadatadatadatadatadatadata L45543443
datadatadatadatadatadatadatadata L45543443
datadatadatadatadatadatadatadata L45543443
datadatadatadatadatadatadatadata L45543443
datadatadatadatadatadatadatadata L45543443
datadatadatadatadatadatadatadata "Z" KEY2
.........
this is my working java code to read all lines from this file :
Path inputFile;
inputFile = Paths.get("inputDir", "myfileName");
fileLines = Files.readAllLines(inputFile, StandardCharsets.ISO_8859_1);
fileLines.forEach(System.out::println);
now my issue is , i want to read each block of line in a specific array dynamically (number of arrays equal number of block lines), a block of lines is constitued by line that contains "X" and ends with line contains "Z" , in my example i have tree blocks of lines , and i have to create 3 arrays each array should contains lines of each block.
can you please guide my on how i can achieve this using java 8 lambda methods.
Thanks in advance.
I resolve my Problem using the solution in this ticket : Java 8: How to convert List<String> to Map<String,List<String>>?
this is my final Code :
Map<String, List<String>> map = fichierInialLines.stream()
.collect(
Collectors.groupingBy(line -> line.substring(0,29),
Collectors.mapping(line -> line, Collectors.toList()))
);
Some old school java code:
List<String> fileLines = new ArrayList<>();
List<List<String>> records=new ArrayList<>();
List<String> tempRecord=null;
for(String fileLine:fileLines)
{
if(fileLine.contains("\"X\""))
{
tempRecord=new ArrayList<>();
tempRecord.add(fileLine); // add X line if necessary
}
else if (fileLine.contains("\"Y\""))
{
tempRecord.add(fileLine); // add Y line if necessary
records.add(tempRecord);
tempRecord=null;
} else {
tempRecord.add(fileLine);
}
}
// every entry in records contains a group of lines
Oops: noticed you asked for a lambda way. sorry
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.