martes, 27 de mayo de 2014

Android

android-library-projects-cannot-be-launched

http://stackoverflow.com/questions/4282552/android-library-projects-cannot-be-launched

Split, StringBuffer

                        tipoVentas= "1_2_3";
System.out.println("Tipo Ventas: "+ tipoVentas);

String[] tipoVenta = null;
tipoVenta = tipoVentas.trim().split("_");
StringBuffer tpventa = new StringBuffer();
for (String string : tipoVenta) {
tpventa.append(string);
System.out.println("tpVenta: "+ tpventa);
}

El resultado es:


Tipo Ventas: 1_2_3
tpVenta: 1
tpVenta: 12
tpVenta: 123

File in Java

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:
CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
tipoVentas = nextLine[5].trim(); //Elimina espacios.