Generate file . txt with three columns

Asked

Viewed 585 times

3

I have a system where I do some research at the bank and store everything in one Array, but I have to put together a layout with this data so that they can be printed as labels on a matrix printer.

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

I need to leave it that way:

inserir a descrição da imagem aqui

What I can get these days is this:

inserir a descrição da imagem aqui

In this case my query returned only two results, the right would be to put this second line in a column as in the first image.

This is my class that generates the txt:

public static void gerarTxt(List<Contrato> lista) {

        try {
            FileWriter arq = new FileWriter("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

Example of the Github project: https://github.com/sinkz/EtiquetaTeste

  • Look that question or this one in the Soen.

  • I think that would solve it, but how to pass the next item on the list in the second column?

1 answer

3


If you know the width of the Column and it is fixed (I assume so, because it is for a matrix printer), you can use the method Stringutils.rightPad.

Either way you will have to work with more than one list record 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 size of the array and the index to avoid outofboud.

Anything pastes your code and data examples to enrich the example.

Good luck

  • Yes, the column is fixed. I’ll try this way and see if it’s right

  • 1

    Anyone shares the code in the editor of their choice and we take a look.

  • A question, you know how I could standardize this layout with Stirngutils?

  • I want to always leave in the same position, I have to measure the size of the previous string before doing the next one?

  • Eu imagino algo assim:&#xA;StringUtils.rightPad(c1.getNome(), 15);&#xA;StringUtils.rightPad(c2.getNome(), 15);&#xA;//quebra uma linha&#xA;StringUtils.rightPad(c1.getContrato(), 15);&#xA;StringUtils.rightPad(c2.getContrato(), 15);&#xA;//quebra uma linha&#xA;It will truncate or pad to the given size. What you mean by Layout?

  • Got it, I’ll try it this way.

  • For example, I can have String’s with different sizes, I have to make that regardless of the size of the String the layout never change, I’m using leftPad and is getting out of order depending on the size, I think it does not consider the white space.

  • I’ll go out to travel :/. Monday I keep trying, the columns are already working just need to line up. Thanks for the help, happy new year.

  • According to your image you must use the rightPad and 3 elements (Contacts) simultaneously. I checked the api, if the size is larger than expected, it will not truncate, then use substring to break in size. We talked on Monday.

  • I made an example project and put it on github if you want to take a look: https://github.com/sinkz/tiThis

  • there’s a file there .txt the first layout is generated by the system, and the bottom part is as I am trying to leave. At first I am trying to do with two columns.

  • I think I got it right, but now I’m having problems with the index :p kk

  • Problem solved, I used rightPad even instead of left

  • Cool. Sorry for the delay, I only got access to the internet now. Thanks

  • Calm down, I poked around and got it. kkk

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.