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

  • Home
  • SEARCH
  • 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 6876573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:29:39+00:00 2026-05-27T04:29:39+00:00

I have some questions regarding reading and writing to CSV files (or if there

  • 0

I have some questions regarding reading and writing to CSV files (or if there is a simpler alternative).

Scenario:
I need to have a simple database of people and some basic information about them. I need to be able to add new entries and search through the file for entries. I also need to be able to find an entry and modify it (i.e change their name or fill in a currently empty field).

Now I’m not sure if a CSV reader/writer is the best route or not? I wouldn’t know where to begin with SQL in Java but if anyone knows of a good resource for learning that, that would be great.

Currently I am using SuperCSV, I put together a test project based around some example code:

class ReadingObjects {

//  private static UserBean userDB[] = new UserBean[2];
private static ArrayList<UserBean> arrUserDB = new ArrayList<UserBean>();

static final CellProcessor[] userProcessors = new CellProcessor[] {
    new StrMinMax(5, 20),
    new StrMinMax(8, 35),
    new ParseDate("dd/MM/yyyy"),
    new Optional(new ParseInt()),
    null
};

public static void main(String[] args) throws Exception {
    ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
    try {
      final String[] header = inFile.getCSVHeader(true);
      UserBean user;
      int i = 0;
      while( (user = inFile.read(UserBean.class, header, userProcessors)) != null) {
        UserBean addMe = new UserBean(user.getUsername(), user.getPassword(), user.getTown(), user.getDate(), user.getZip());

        arrUserDB.add(addMe);
        i++;

      }
    } finally {
      inFile.close();
    }

    for(UserBean currentUser:arrUserDB){
        if (currentUser.getUsername().equals("Klaus")) {
            System.out.println("Found Klaus! :D");
        }
    }

    WritingMaps.add();
   }
}

And a writer class:

class WritingMaps {

  public static void add() throws Exception { 
ICsvMapWriter writer = new CsvMapWriter(new FileWriter("foo.csv", true), CsvPreference.EXCEL_PREFERENCE);
try {
  final String[] header = new String[] { "username", "password", "date", "zip", "town"};
  String test = System.getProperty("line.seperator");


  // set up some data to write
  final HashMap<String, ? super Object> data1 = new HashMap<String, Object>();
  data1.put(header[0], "Karlasa");
  data1.put(header[1], "fdsfsdfsdfs");
  data1.put(header[2], "17/01/2010");
  data1.put(header[3], 1111);
  data1.put(header[4], "New York");


  System.out.println(data1);

  // the actual writing
//      writer.writeHeader(header);
  writer.write(data1, header);
//      writer.write(data2, header);
} finally {
  writer.close();
}
  }
}

Issues:
I’m struggling to get the writer to add a new line to the CSV file. Purely for human readability purposes, not such a big deal.
I’m not sure how I would add data to an existing record to modify it. (remove and add it again? Not sure how to do this).

Thanks.

  • 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-27T04:29:40+00:00Added an answer on May 27, 2026 at 4:29 am

    The easiest solution is to read the file at application startup into an in-memory structure (list of UserBean, for example), to add, remove, modify beans in this in-memory structure, and to write the whole list of UserBean to the file when the app closes, or when the user chooses to Save.

    Regarding newlines when writing, the javadoc seems to indicate that the writer will take care of that. Just call write for each of your user bean, and the writer will automatically insert newlines between each row.

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

Sidebar

Related Questions

There have been some similar questions asked regarding Grid views, but none have been
I have some questions regarding the following css that I found: html, body {
I've read some documentation on how Adaboost works but have some questions regarding it.
I have couple questions regarding some C++ rules. Why am I able to call
I have some questions about importing data from Excel/CSV File into SQL Server. Let
I have some questions regarding java finalization. For example I have one class FileHelper
I have some question over here regarding the java garbage collector. First let me
I have a question regarding the some data which is being transfered from one
I have an other active question HERE regarding some hopeless memory issues that possibly
I have some questions concering routing with Codeigniter. What I´m doing now is the

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.