Delete last line - Java

Asked

Viewed 895 times

1

I’m reading a CSV using Bufferedreader by reading line by converting the result into a .txt. At the end of my txt is generating a line break after the last item. I’m trying to find a way to remove last line break after the last item on the list.

My CSV file has 50 lines. Generated txt file has 51 lines.

public static void converter() throws Exception  {
    int EXPECTED = 15;      
    String fileName = txtCaminho.getText();
    String gerado = txtLocal.getText();
    BufferedReader reader = new BufferedReader(new FileReader(fileName));

    int x = 0;

    String aLine ;



   while ((aLine = reader.readLine()) != null)
    {

      StringTokenizer token = new StringTokenizer(aLine, ",");
      if (token.countTokens() != EXPECTED) {
          JOptionPane.showMessageDialog(null, erro_messagem);
        throw new Exception("Illegal column count: " + token.countTokens());
      }
      x++;
      String form = token.nextToken();
      String ra = token.nextToken();
      String tipo = token.nextToken();   
      String q1 = token.nextToken();
      String q2 = token.nextToken();
      String q3 = token.nextToken();
      String q4 = token.nextToken();
      String q5 = token.nextToken();
      String q6 = token.nextToken();
      String q7 = token.nextToken();
      String q8 = token.nextToken();
      String q9 = token.nextToken();
      String q10 = token.nextToken();
      String q11 = token.nextToken();
      String q12 = token.nextToken();


      String newOrder = ra+";"+tipo+";" +q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10 +q11+q12;

      String newBuffer = newOrder;

      System.out.print(newBuffer.replaceAll("\n", ""));

      gravar(gerado, newBuffer );
      System.out.println(x);
        }
      }

Write function ()

public static void gravar(String path, String texto)
  {
    try
    {     
      FileWriter fw = new FileWriter(path, true);
      BufferedWriter conexao = new BufferedWriter(fw);
      conexao.write(texto);
      conexao.newLine();

      conexao.close();

      txtCaminho.setText("");
      txtLocal.setText("");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

1 answer

1


The problem is that in your "record" method you are forcing the "break line" right after writing the text.

One of the ways to avoid this, instead of replacing n, is to avoid line breaking. The method ready() Bufferedreader class will help you:

public void gravar(String path, String texto, boolean deveQuebrarLinha) {
  FileWriter fw = new FileWriter(path, true)
  BufferedWriter conexao = new BufferedWriter(fw);
  conexao.write(texto);

  if (deveQuebrarLinha)
    conexao.newLine();
}

And in your reading method:

 public void converter() {
    String line;
    BufferedReader reader = new BufferedReader(new FileReader("seu-arquivo.txt"));
    while((line = reader.readLine()) != null) {         
        gravar("path", line, reader.ready());
    }
 }

Documentation: https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#ready()

  • Thank you, as expected.

Browser other questions tagged

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