Generate three-column .txt file

3

I have a system where I do some search in the database and store everything in Array , however I have to assemble a layout with this data so they can be printed as labels in a dot matrix printer.

The part of the lines I have already formatted, but I do not know how to make the next record in the list instead of going to the next line turn a column.

I need to leave it this way:

WhatIcangetnowisthis:

Inthiscasemyqueryreturnedonlytworesults,therightthingwouldbetoputthatsecondrowinacolumnasinthefirstimage.

Thisismyclassthatgeneratestxt:

publicstaticvoidgerarTxt(List<Contrato>lista){try{FileWriterarq=newFileWriter("C:\etiqueta.txt");
            PrintWriter gravarArq = new PrintWriter(arq);
            for (Contrato item : lista) {
                gravarArq.print(item.getContrato());
                gravarArq.print("\r\n");
                gravarArq.print(item.getContratante());
                gravarArq.print("\r\n");
                gravarArq.print(item.getRua().trim()+", "+item.getNumero());
                gravarArq.print("\r\n");
                gravarArq.print(item.getBairro());
                gravarArq.print("\r\n");
                gravarArq.print(item.getCep() + " "+item.getCidade()+ " "+StringUtils.leftPad(item.getUf(), 22));
                gravarArq.print("\r\n");
                gravarArq.print(".");
                gravarArq.print("\r\n");
            }
            arq.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

How can I turn these rows into columns?

Update

Sample project in GitHub: link

    
asked by anonymous 30.12.2015 / 19:58

1 answer

3

If you know the width of the column and it is fixed (I assume so, as it is for a dot matrix printer), you can use the StringUtils.rightPad .

Either way you will have to work with more than one record in the list at the same time. For example:

for (int i = 0; i < lista.size(); i + 2) {
    Contato c1 = lista.get(i);
    Contato c2 = lista.get(i + 1);

    metodoQueVaiEscreverOText(c1, c2);

}

Still, you need to control the array size and index, to avoid outofboud.

Anything glue your code and sample data to enrich the example.

Good luck

    
31.12.2015 / 12:30