Created
April 11, 2012 19:30
-
-
Save thiagotn/2361735 to your computer and use it in GitHub Desktop.
Csv Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package migrator.util; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.StringTokenizer; | |
import com.google.common.base.Charsets; | |
import com.google.common.base.Strings; | |
import com.google.common.io.Files; | |
public class CsvUtil { | |
public static List<String> processaLinha(String data, int totalColunas) { | |
List<String> columns = new ArrayList<String>(totalColunas); | |
StringTokenizer tokenizer = new StringTokenizer(data, ";"); | |
while (tokenizer.hasMoreTokens()) { | |
String column = tokenizer.nextToken(); | |
columns.add(column); | |
} | |
return columns; | |
} | |
public static String writeValue(String value) { | |
return Strings.isNullOrEmpty(value) ? "\"\"" : value; | |
} | |
public static void processaArquivo(String arquivo) throws IOException { | |
File file = new File(arquivo); | |
List<String> linhas = Files.readLines(file, Charsets.UTF_8); | |
for (String linha : linhas) { | |
List<String> colunas = processaLinha(linha, 3); | |
for (String coluna : colunas) { | |
System.out.print(coluna + " "); | |
} | |
System.out.println("\n"); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
processaArquivo(".\\src\\main\\resources\\users_basic_fc_10042012.csv"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment