import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
public class ExampleClass {
public static void main(String[] args) throws IOException {
1. READING A FILE INPUT STREAM:
FileInputStream file= new FileInputStream("c://op_modelo_plan.CSV");
InputStreamReader cvs = new InputStreamReader(file);
CSVReader reader = new CSVReader(cvs, ';', '"', 1); //Aqui comienza desde la segunda fila del excel.
//CSVReader reader = new CSVReader(cvs, ';', '"', 0); //Si se quiere comenzar desde la primera hay q cambiar a 0
System.out.println("El archivo ya fue del tipo fileReader");
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0] +","+ nextLine[1]+","+nextLine[2]+","+ nextLine[3]);
//System.out.println("Name: [" + nextLine[1] + "]\nAddress: [" + nextLine[2] + "]\nEmail: [" + nextLine[3] + "]");
}
2. ANOTHER WAY TO READ A FILE INPUT STREAM
private static final String ADDRESS_FILE = "c:\\op_modelo_plan.CSV";
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
System.out.println("El archivo ya fue del tipo fileReader");
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0]);
//System.out.println("Name: [" + nextLine[1] + "]\nAddress: [" + nextLine[2] + "]\nEmail: [" + nextLine[3] + "]");
}
System.out.println("After read it");
// Try writing it back out as CSV to the console
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List allElements = reader2.readAll();
3. WRITING IT ON SCREEN
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(allElements);
System.out.println("\n\nGenerated CSV File:\n\n");
System.out.println(sw.toString());
}
Another useful code:
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
public class ExampleClass {
public static void main(String[] args) throws IOException {
1. READING A FILE INPUT STREAM:
FileInputStream file= new FileInputStream("c://op_modelo_plan.CSV");
InputStreamReader cvs = new InputStreamReader(file);
CSVReader reader = new CSVReader(cvs, ';', '"', 1); //Aqui comienza desde la segunda fila del excel.
//CSVReader reader = new CSVReader(cvs, ';', '"', 0); //Si se quiere comenzar desde la primera hay q cambiar a 0
System.out.println("El archivo ya fue del tipo fileReader");
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0] +","+ nextLine[1]+","+nextLine[2]+","+ nextLine[3]);
//System.out.println("Name: [" + nextLine[1] + "]\nAddress: [" + nextLine[2] + "]\nEmail: [" + nextLine[3] + "]");
}
2. ANOTHER WAY TO READ A FILE INPUT STREAM
private static final String ADDRESS_FILE = "c:\\op_modelo_plan.CSV";
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
System.out.println("El archivo ya fue del tipo fileReader");
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0]);
//System.out.println("Name: [" + nextLine[1] + "]\nAddress: [" + nextLine[2] + "]\nEmail: [" + nextLine[3] + "]");
}
System.out.println("After read it");
// Try writing it back out as CSV to the console
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List
3. WRITING IT ON SCREEN
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(allElements);
System.out.println("\n\nGenerated CSV File:\n\n");
System.out.println(sw.toString());
}
Another useful code:
CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
tipoVentas = nextLine[5].trim(); //Elimina espacios.
No hay comentarios:
Publicar un comentario