Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:11:30+00:00 2026-05-15T21:11:30+00:00

My first CSV file looks like this with header included (header is included only

  • 0

My first CSV file looks like this with header included (header is included only at the top not after every entry):

NAME,SURNAME,AGE
Fred,Krueger,Unknown
.... n records

My second file might look like this:

NAME,MIDDLENAME,SURNAME,AGE
Jason,Noname,Scarry,16
.... n records with this header template

The merged file should look like this:

NAME,SURNAME,AGE,MIDDLENAME
Fred,Krueger,Unknown,
Jason,Scarry,16,Noname
....

Basically if headers don’t match, all new header titles (columns) should be added after original header and their values according to that order.

Update

Above CSV were made smaller so I can illustrate what I want to achieve, in reality CSV files are generated one step before this (merge) and can be up to 100 columns

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:11:31+00:00Added an answer on May 15, 2026 at 9:11 pm

    I’d create a model for the ‘bigger’ format (a simple class with four fields and a collection for instances of this class) and implemented two parsers, one for the first, one for the second model. Create records for all rows of both csv files and implement a writer to output the csv in the correct format. IN brief:

     public void convert(File output, File...input) {
    
       List<Record> records = new ArrayList<Record>();
       for (File file:input) {
         if (input.isThreeColumnFormat()) {
            records.addAll(ThreeColumnFormatParser.parse(file));
         } else {
            records.addAll(FourColumnFormatParser.parse(file));
         }
       }
       CsvWriter.write(output, records);
     }
    

    From your comment I see, that you a lot of different csv formats with some common columns.

    You could define the model for any row in the various csv files like this:

    public class Record {
      Object id; // some sort of unique identifier
      Map<String, String> values; // all key/values of a single row
      public Record(Object id) {this.id=id;}
      public void put(String key, String value){
        values.put(key, value);
      }
      public void get(String key) {
        values.get(key);
      }
    }
    

    For parsing any file you would first read the header and add the column headers to a global keystore (will be needed later on for outputting), then create records for all rows, like:

    //...
    List<Record> records = new ArrayList<Record>()
    
    for (File file:getAllFiles()) {
      List<String> keys = getColumnsHeaders(file);
      KeyStore.addAll(keys);  // the store is a Set
      for (String line:file.getLines()) {
        String[] values = line.split(DELIMITER);
        Record record = new Record(file.getName()+i);  // as an example for id
        for (int i = 0; i < values.length; i++) {
          record.put(keys.get(i), values[i]);
        }
        records.add(record);
      }
    }
    // ...
    

    Now the keystore has all used column header names and we can iterate over the collection of all records, get all values for all keys (and get null if the file for this record didn’t use the key), assemble the csv lines and write everything to a new file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load a CSV file that looks like this: Acct. No.,1-15 Days,16-30
I have a csv file that looks something like this (actual file has many
I would like to parse a CSV file so that each row is treated
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.
First off, there's a bit of background to this issue available on my blog:
First let me say that I really feel directionless on this question. I am
Short question: How do I automatically detect whether a CSV file has headers in
Is it possible to BULK INSERT (SQL Server) a CSV file in which the
If i have a CSV file that is in the following format fd!,sdf,dsfds,dsfd fd!,asdf,dsfds,dsfd
I am reading in a csv file in Java and, depending on the format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.