CSV in MS-DOS JAVA format

0

I have a problem when generating my file CSV from JAVA in the OS SunOS 5.10 (in Windows it works correctly) the format I need is MS-DOS try what the following question from the SO English tells me:

  

link

But it does not work for me, also try with the following code taken from the forum in English:

public final static char CR  = (char) 0x0D;
public final static char LF  = (char) 0x0A; 

public final static String CRLF  = "" + CR + LF;     // "" forces conversion to string

String twoLines = "Line1" + CRLF + "Line2";   // 12 characters

Source:

  

link

The same try with:

System.getProperty("line.separator")

But by no means did it come to me. I share my code that generates the CSV :

archivoCsv = new CsvWriter(new FileWriter(new File(System.getProperty("user.dir")+"/../salida/"+campaniasBL.nombreArchivoExtraccion),false), ',');
archivoCsv.write("Folio de Campaña");
archivoCsv.write("Nombre de Campaña");
archivoCsv.write("Inicio");
archivoCsv.write("Fin");
archivoCsv.endRecord();
if (archivoCsv != null) {
    archivoCsv.close();
}

I hope you can help me.

    
asked by J. Castro 09.01.2017 в 22:12
source

1 answer

0

I share the solution to my problem in case someone in the future is in the same situation as me:

public class CsvWriter {

    private static final String COMMA_DELIMITER = ",";
    private static final String NEW_LINE_SEPARATOR = "\r\n";
    private static final String FILE_HEADER = "id,firstName,lastName,gender,age";

    public static void main(String[] args) {
        FileWriter fileWriter = null;
        //Nombre y directorio del archivo a crear
        String fileName = System.getProperty("user.dir") + "/csvMsDosformat.csv";
        try {
            fileWriter = new FileWriter(fileName);
            fileWriter.append(FILE_HEADER.toString());
            fileWriter.append(NEW_LINE_SEPARATOR);
            for (int x = 0; x < 10; x++) {
                fileWriter.append("Prueba");
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append("Prueba 1");
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append("Prueba 2");
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append("Prueba 3");
                fileWriter.append(COMMA_DELIMITER);
                fileWriter.append("Prueba 4");
                fileWriter.append(NEW_LINE_SEPARATOR);
            }
            System.out.println("CSV creado correctamente");

        } catch (Exception e) {
            System.out.println("Error al crear CSV");
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                System.out.println("Error al cerrar el CSV");
                e.printStackTrace();
            }
        }
    }
}

Source:

  

link

    
answered by 10.01.2017 / 14:37
source