csv file 1:
a|b|c
a|a|a
b|b|b
csv file 2:
a|b|c
c|c|c
d|d|d
output csv file:
a|b|c
a|a|a
b|b|b
c|c|c
d|d|d
here's my code i tried to merge but the header is repeating twice : and when it comes to merging two files into single alternate csv file then also iam facing the same issue as the above the header columns gets repeated everytime what am i supposed to do to ignore it. here's my code below
public static void main(String[] args) throws IOException
{
// PrintWriter object for file3.txt
PrintWriter pw = new PrintWriter("file3.txt");
// BufferedReader object for file1.txt
BufferedReader br1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("file2.txt"));
String line1 = br1.readLine();
String line2 = br2.readLine();
// loop to copy lines of
// file1.txt and file2.txt
// to file3.txt alternatively
while (line1 != null || line2 !=null)
{
if(line1 != null)
{
pw.println(line1);
line1 = br1.readLine();
}
if(line2 != null)
{
pw.println(line2);
line2 = br2.readLine();
}
}
pw.flush();
// closing resources
br1.close();
br2.close();
pw.close();
howw to skip the header column in the output file
Just skip the first lines of second and subsequent CSV files:
for (int i = 0; i < fileNames.length; i++) {
System.out.println("Reading from " + fileNames[i]);
File f = new File(dir, fileNames[i]);
BufferedReader br = new BufferedReader(new FileReader(f));
String line = br.readLine();
if (i > 0)
line = br.readLine(); //just skip the first line
while (line != null) {
pw.println(line);
line = br.readLine();
}
pw.flush();
}
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.