Delete Last Row - Java

1

I'm reading a CSV using BufferedReader reading row by line converting the result to 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 in the list.

My CSV file has 50 lines. File generated txt 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);
        }
      }

Record 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();
    }
  }
    
asked by anonymous 05.10.2017 / 16:29

1 answer

1

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

One way to avoid this, instead of doing the \ n substitution, is to avoid the line break. The ready () method of the 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: link

    
05.10.2017 / 17:41