How to merge two csv files with same header columns into another csv file using javaHow can I concatenate two arrays in Java?How do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How do I create a file and write to it in Java?How to avoid Java code in JSP files?How to sort CSV file by header?Merging a bunch of csv files into one with headersExtract and merge columns in a CSVcompare a set of strings from one csv file in another csv file using Unix Shell scriptScript to map columns in two csv files
The IT department bottlenecks progress. How should I handle this?
Creepy dinosaur pc game identification
Not using 's' for he/she/it
When were female captains banned from Starfleet?
Basic combinatorial probability problem
How could a planet have erratic days?
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?
Why did the EU agree to delay the Brexit deadline?
Open a doc from terminal, but not by its name
What changes for testers when they are testing in agile environments?
How to create ADT in Haskell?
How to explain what's wrong with this application of the chain rule?
Explaining alternative travel routes when going to the USA
Lowest total scrabble score
Python scanner for the first free port in a range
Are the IPv6 address space and IPv4 address space completely disjoint?
Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?
Can I sign legal documents with a smiley face?
Is aluminum electrical wire used on aircraft?
Travelling outside the UK without a passport
Is (0,1] a closed or open set?
Is there any references on the tensor product of presentable (1-)categories?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
How to merge two csv files with same header columns into another csv file using java
How can I concatenate two arrays in Java?How do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How do I create a file and write to it in Java?How to avoid Java code in JSP files?How to sort CSV file by header?Merging a bunch of csv files into one with headersExtract and merge columns in a CSVcompare a set of strings from one csv file in another csv file using Unix Shell scriptScript to map columns in two csv files
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
java performance csv
add a comment |
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
java performance csv
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago
add a comment |
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
java performance csv
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
java performance csv
java performance csv
edited 2 days ago
pragadeeshwaran venkatachalam
asked 2 days ago
pragadeeshwaran venkatachalampragadeeshwaran venkatachalam
34
34
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago
add a comment |
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
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();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281263%2fhow-to-merge-two-csv-files-with-same-header-columns-into-another-csv-file-using%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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();
add a comment |
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();
add a comment |
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();
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();
edited 2 days ago
Jaja
516
516
answered 2 days ago
Pavel SmirnovPavel Smirnov
1,177214
1,177214
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281263%2fhow-to-merge-two-csv-files-with-same-header-columns-into-another-csv-file-using%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you want a file with only the unique lines of all input files?
– Robert Kock
2 days ago
Are the input files sorted? Is the order of the lines in the output file important?
– Robert Kock
2 days ago
no the order is not important but the data should not be skipped if they have duplicates they should not be skipped
– pragadeeshwaran venkatachalam
2 days ago
i have added another code with two files merged to single file its also getting the same issue how to ignore the header column
– pragadeeshwaran venkatachalam
2 days ago