CSV Tokenizer Util : CSV File « Development Class « Java






CSV Tokenizer Util

/* * Copyright (c) 2008-2011 Simon Ritchie. * All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see http://www.gnu.org/licenses/>. *///package org.rimudb.util; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; /** * @author Simon Ritchie * */publicclass TokenizerUtil { publicstatic String[] convertCSVStringToArray(String s) { ArrayList<String> list = new ArrayList<String>(); StringTokenizer st = new StringTokenizer(s, "|"); while (st.hasMoreTokens()) { String token = st.nextToken(); list.add(token); } return (String[]) list.toArray(new String[list.size()]); } publicstatic String convertArrayToCSVString(String[] s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length; i++) { if (i > 0) { sb.append("|"); } sb.append(s[i]); } return sb.toString(); } publicstatic String convertMapToCSVString(Map<String, String> urlDefaultMap) { StringBuffer sb = new StringBuffer(); Iterator<String> it = urlDefaultMap.keySet().iterator(); while (it.hasNext()) { if (sb.length() > 0) { sb.append("|"); } String jdbcDriver = it.next(); String defaultURL = urlDefaultMap.get(jdbcDriver); sb.append(jdbcDriver+"|"+defaultURL); } return sb.toString(); } publicstatic Map<String, String> convertCSVStringToMap(String s) { TreeMap<String, String> map = new TreeMap<String, String>(); StringTokenizer st = new StringTokenizer(s, "|"); while (st.hasMoreTokens()) { String keyToken = st.nextToken(); String valueToken = st.nextToken(); map.put(keyToken, valueToken); } return map; } } 








Related examples in the same category

1.A utility class that parses a Comma Separated Values (CSV) file
2.Simple demo of CSV parser classSimple demo of CSV parser class
3.CSV in action: lines from a file and printCSV in action: lines from a file and print
4.Simple demo of CSV matching using Regular Expressions
5.Helper class to write table data to a csv-file (comma separated values).
6.Builds a bracketed CSV list from the array
7.Builds a CSV list from the specified String[], separator string and quote string
8.Builds a CSV list from the specified two dimensional String[][], separator string and quote string.
9.The csv tokenizer class allows an application to break a Comma Separated Value format into tokens.
10.The CSVQuoter is a helper class to encode a string for the CSV file format.
11.A stream based parser for parsing delimited text data from a file or a stream
12.Reads CSV (Comma Separated Value) files
13.Writes CSV (Comma Separated Value) files
14.Csv Converter
15.CVS reader
16.CSV Writer
17.CSV parser
18.Csv Reader
19.A very simple CSV parser released under a commercial-friendly license.
20.A very simple CSV reader released under a commercial-friendly license.
21.A very simple CSV writer released under a commercial-friendly license.
22.CSV file reader
23.CSV file writer
24.Parse a line of text in CSV format and returns array of Strings Implementation of parsing is extracted from open-csv.
25.CSV Writer
26.Parse comma-separated list of ints and return as array
27.Parse comma-separated list of longs and return as array
close