I need to merge the data in two CSV file with similar header but different data according to the their respective section. The CSV file will contain two sections,the first part contain the data according sector and the second part will contain the data from residential area. Here is my first CSV file
Sector,Total Number of Occurrence
sector1,12
sector2,30
sector3,100
House,Total Number of Occurrence
B12,80
A2,87
My second CSV file
Sector,Total Number of Occurrence
sector 99,89
sector 11,9
House,Total Number of Occurrence
Q11,22
Q22,67
I hope to generated a CSV file contain both data inside but the data must be allocated to the correct section which look like the following
Sector,Total Number of Occurrence
sector1,12
sector2,30
sector3,100
sector 99,89
sector 11,9
House,Total Number of Occurrence
B12,80
A2,87
Q11,22
Q22,67
But I guess with my current developed source code, it's cant able to do that because it's contain the second header listed inside the CSV which are House,Total Number of Occurrence. May I know how to achieve my desired output? This is what my current csv output look like
Sector,Total Number of Occurrence
sector1,12
sector2,30
sector3,100
House,Total Number of Occurrence
B12,80
A2,87
sector 99,89
sector 11,9
House,Total Number of Occurrence
B12,80
A2,87
This is my current developed source code
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SummarizeReport1
{
static ArrayList<String> list1 = new ArrayList<>();
static String line1;
public static void main(String[] args) throws IOException
{
List<Path> paths = Arrays.asList(Paths.get("C:\\Users\\user\\Desktop\\file\\log\\backup\\report12017-10-31.csv"), Paths.get("C:\\Users\\user\\Desktop\\file\\log\\backup\\report12017-10-31 - Copy.csv"));
List<String> mergedLines = getMergedLines(paths);
Path target = Paths.get("C:\\Users\\user\\Desktop\\file\\log\\backup\\SummarizedReport1.csv");
Files.write(target, mergedLines, Charset.forName("UTF-8"));
}
private static List<String> getMergedLines(List<Path> paths) throws IOException
{
List<String> mergedLines = new ArrayList<> ();
for (Path p : paths)
{
List<String> lines = Files.readAllLines(p, Charset.forName("UTF-8"));
if (!lines.isEmpty()) {
if (mergedLines.isEmpty()) {
mergedLines.add(lines.get(0)); //add header only once
}
mergedLines.addAll(lines.subList(1, lines.size()));
}
}
return mergedLines;
}
}