Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

Java List to CSV

14

    Brief Description of CSV Files

    • A comma separated values file stores collections of data in a manner quite similar to a spreadsheet. An object is defined on a single row, and each column contains a particular field from the object. To ensure the data is easy to parse, a special character -- often a comma but any character can be used -- is inserted between each column. Optionally, some programs, such as Excel, will include a header row at the top, that indicates what each column represents.

    Creating a CSV File

    • Creating a CSV file is easy. In fact, the most difficult part is often determining what character to use. As stated above, the comma is a frequently chosen character. Here's an example of creating a CSV file using commas:

      final List<Contact> contacts = getContacts();

      final FileWriter fw = new FileWriter(fileName);

      for (final Contact c : contacts) {

      final String csvStr = "\"" + c.getFirstName() + "\",\"" + c.getLastName() + "\",\"" + c.getPhoneNumber() + "\"";

      fw.write(csvStr + "\n");

      }

      fw.close();

      Note that this method adds double quotes (") around each value; this is so the values can be read properly if there happens to be a value that contains a comma.

    Reading CSV Files

    • Reading data from a CSV file is likewise simple, provided that you know the order the data is stored in the file. Unlike other options, such as XML or relational databases, CSV files generally don't contain any information about the data that's stored.

      final FileReader fr = new FileReader(fileName);

      final BufferedReader br = new BufferedReader(fr);

      final List<Contact> contacts = new ArrayList<Contact>();

      String line;

      while ((line = br.readLine()) != null) {

      final String[] parts = line.split("\",\"");

      parts[0] = parts[0].replace("\"", "");

      parts[2] = parts[2].replace("\"", "");

      contacts.add(new Contact(parts[0], parts[1], parts[2]));

      }

      Note that the code explicitly replaces all double quotes in the first name (parts[0]) and phone numbers (parts[2]); this is strictly for brevity in this article. More robust code should only remove the starting and ending quotes from each, respectively. The code should also include more appropriate error checking and handling as well, that is, to ensure that the returned array truly does have all parts.

    Deciding When to Use CSV Files

    • CSV is a simple -- and easy -- option. However, knowing when to use it is key as well. If the dataset is going to be large, it may be more appropriate to use a relational database so you can take advantage of additional querying and modification support. If the data is going to be shared with other developers, XML might be a reasonable choice as well, as it is not only simple to use in most cases, but it also can more easily incorporate additional information about the data stored, as in having a tag "<firstName>Bob</firstName> instead of just a column in a text file.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.