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();
}
}