Java

Chances are you might have needed to convert a list of strings or numbers to a CSV file while you were programming something. An example is that in any java program you might have obtained a list of states of United States stored in your...
Chances are you might have needed to convert a list of strings or numbers to a CSV file while you were programming something. An example is that in any java program you might have obtained a list of states of United States stored in your ArrayList object and then you wanted to have them in a CSV format so that you probably could load it to a database or use it for some other purposes. I wrote this tool to serve the same purpose. Here are the basic features this simple java example can do. Given a list of String objects stored in an ArrayList, this program can: Convert Strings or numbers stored in an ArrayList object to comma separated strings Print the comma separated values (CSV) to either console or file Optionally you can sort the the list before you do the conversion. package com.kushal.tools; import java.io.BufferedWriter; import java.io.FileWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * @author Kushal Paudyal * Last Modified on 2011-09-06 This utility converts a * list to comma separated values. Intended to be used with Strings and * can be modified with numbers. * * Have options to write the converted values to either console or file. */ public class ListToCSV { private static boolean writeCSVToConsole = true; private static boolean writeCSVToFile = true; private static String destinationCSVFile = "C:\\temp\\convertedCSV.csv"; private static boolean sortTheList = true; public static void main(String[] args) { ListToCSV util = new ListToCSV(); List sampleList = util.createSampleList(); util.convertAndPrint(sampleList, writeCSVToConsole, writeCSVToFile, sortTheList); } /** * @param sampleList - input list of string * @param writeToConsole - if this flag is true, writes to console * @param writeToFile - if this flag is true writes to file. * @param sortTheList - if the list is to be sorted before conversion */ private void convertAndPrint(List sampleList, boolean writeToConsole, boolean writeToFile, boolean sortTheList) { String commaSeparatedValues = ""; /** If the list is not null and the list size is not zero, do the processing**/ if (sampleList != null) { /** Sort the list if sortTheList was passed as true**/ if(sortTheList) { Collections.sort(sampleList); } /**Iterate through the list and append comma after each values**/ Iterator iter = sampleList.iterator(); while (iter.hasNext()) { commaSeparatedValues += iter.next() + ","; } /**Remove the last comma**/ if (commaSeparatedValues.endsWith(",")) { commaSeparatedValues = commaSeparatedValues.substring(0, commaSeparatedValues.lastIndexOf(",")); } } /** If writeToConsole flag was passed as true, output to console**/ if(writeToConsole) { System.out.println(commaSeparatedValues); } /** If writeToFile flag was passed as true, output to File**/ if(writeToFile) { try { FileWriter fstream = new FileWriter(destinationCSVFile, false); BufferedWriter out = new BufferedWriter(fstream); out.write(commaSeparatedValues); out.close(); System.out.println("*** Also wrote this information to file: " + destinationCSVFile); } catch (Exception e) { e.printStackTrace(); } } } /** * Creates a sample list to be used by the convertAndPrint method * and returns it to the calling method. */ private List createSampleList() { List sampleList = new ArrayList(); sampleList.add("Nebraska"); sampleList.add("Iowa"); sampleList.add("Illinois"); sampleList.add("Idaho"); return sampleList; } } Originally posted 2011-09-16 18:54:06.
about 5 hours ago
Complexity is the new reality of web and mobile applications with almost no new release going out without the addition of services and applications spread across many different companies. But the reality of this new interrelationship is ...
Complexity is the new reality of web and mobile applications with almost no new release going out without the addition of services and applications spread across many different companies. But the reality of this new interrelationship is still the same: If a third-party Internet outage or issue occurs, your brand is the one that is affected. With up to 1,500 distinct third-party services available to choose from around the world, it is sometimes difficult to even identify what a service does when it appears in your applications. This forces your team to not only be fully aware of the components you control, but also to be able to follow the trail of services that extends far outside the code and systems your company manages when issues appear.read more
about 7 hours ago
But to my mind, though I am native here And to the manner born, it is a custom More honor'd in the breach than the observance - Hamlet Act 1, scene 4, 7–16 Preview Text: Do you know w...
But to my mind, though I am native here And to the manner born, it is a custom More honor'd in the breach than the observance - Hamlet Act 1, scene 4, 7–16 Preview Text: Do you know what a Sprint Review is for, and how it differs from a Retrospective? In this article we look at what a Sprint Review is meant to be, why it is important,...
about 7 hours ago
Authors note: A recent blog post on Bob Sutton’s Work Matters reminded me of this little piece I wrote a while ago. A simple thank you can make a difference; appreciation builds good will, and reminds people that they are valued as h...
Authors note: A recent blog post on Bob Sutton’s Work Matters reminded me of this little piece I wrote a while ago. A simple thank you can make a difference; appreciation builds good will, and reminds people that they are valued as human beings, not just as CPUs (Code Producing Units) or FTEs (Full Time Equivalents). Preview Text: A simple...
about 7 hours ago
A few weeks ago Michael published a post called Product Software Development Is a Marathon. The message of this post is: if you want to come up with a decent product, you’ve got to brace yourself up for a long-distance marathon run. I ag...
A few weeks ago Michael published a post called Product Software Development Is a Marathon. The message of this post is: if you want to come up with a decent product, you’ve got to brace yourself up for a long-distance marathon run. I agree to the point of long-distance and endurance, but I’d rather compare this not to a marathon but to a triathlon race. Preview Text: ...
about 7 hours ago
“Cloud has everything to do with what has happened with Big Data,” explained Jason Deck, Director of Strategic Alliances at Logicworks, in this exclusive Q&A with Cloud Expo Conference Chair Jeremy Geelan. “Big Data doesn’t exist in its ...
“Cloud has everything to do with what has happened with Big Data,” explained Jason Deck, Director of Strategic Alliances at Logicworks, in this exclusive Q&A with Cloud Expo Conference Chair Jeremy Geelan. “Big Data doesn’t exist in its easily accessible way without cloud. From reduced startup costs, to cheap storage, to fast processing, to adequate security, to the easy incorporation of third-party analytics tools, cloud made Big Data accessible to customers of all sizes, with all different budgets.” Cloud Computing Journal: The move to cloud isn't about saving money, it is about saving time. – Agree or disagree? Jason Deck: The move to cloud is not specifically about money or time. The real value of cloud computing is in giving a business the platform to create real operational agility, so an organization can focus economic and human resources on the most valuable parts of its business. Cloud does save time and it does save money but, above all, it provides a company with agility to respond to its competitors and test out new ideas with minimal risk, all while being certain sensitive data are secure and compliant.read more
about 8 hours ago
CERN first made available the Web's source code 20 years ago. Simon Phipps discusses what we can still learn from that spirit of openness today.
CERN first made available the Web's source code 20 years ago. Simon Phipps discusses what we can still learn from that spirit of openness today.
about 11 hours ago
If you do the work, you can get a computer science education online rivaling that of the Ivy League. Here's a nice little curriculum for you.
If you do the work, you can get a computer science education online rivaling that of the Ivy League. Here's a nice little curriculum for you.
about 11 hours ago
Jira 6 comes with a new UI, performance improvements, and the first interface designed specifically for mobile clients.
Jira 6 comes with a new UI, performance improvements, and the first interface designed specifically for mobile clients.
about 11 hours ago
Android mobile developers have reason to celebrate with Google's new Android development environment, which pairs rich layout and build capabilities with IntelliJ IDEA's famous ease.
Android mobile developers have reason to celebrate with Google's new Android development environment, which pairs rich layout and build capabilities with IntelliJ IDEA's famous ease.
about 11 hours ago