将换行符添加到txt文件- Java中

时间:2021-03-19 20:20:00

This is probably a really simple question but im just learning Java and need help. I have a line of sql table writing to txt file but its not adding a linebreak after each append. Its just appending them all together as one word. How do i insert a linebreak after each append? Column is an address so 1 address per line.

这可能是一个非常简单的问题,但我只是在学习Java,需要帮助。我有一行写txt文件的sql表,但它没有在每个附加后添加换行符。它只是把它们加在一起作为一个词。如何在每个附加后插入换行符?列是每行1个地址。

StringBuffer fileContent = new StringBuffer();
TableModel tModel = table.getModel();
for (int i = 0; i < tModel.getRowCount(); i++) {

    Object cellValue = tModel.getValueAt(i, 0);
    // ... continue to read each cell in a row
    fileContent.append(cellValue);

    // ... continue to append each cell value
}

FileWriter fileWriter = new FileWriter(new File("D://properties1.txt"));
fileWriter.write(fileContent.toString());

fileWriter.flush();
fileWriter.close();

4 个解决方案

#1


1  

You can do it as fileContent.append(cellValue + "\n");

你可以用fileContent。追加(cellValue +“\ n”);

#2


0  

just before the end of the loop add fileContent.append("\n");

在循环结束之前添加fileContent.append(“\n”);

#3


0  

use the new line char, \n.

使用新的行char, \n。

where you have this fileContent.append(cellValue);

这里有这个filecontentte .append(cellValue);

should be fileContent.append(cellValue+"\n");

应该fileContent.append(cellValue +“\ n”);

#4


0  

For windows applications like notepad, you need to use a carriage return followed by a new line.

对于像记事本这样的windows应用程序,您需要使用一个回车,后面跟着一行新的行。

fileContent.append("\r\n");

#1


1  

You can do it as fileContent.append(cellValue + "\n");

你可以用fileContent。追加(cellValue +“\ n”);

#2


0  

just before the end of the loop add fileContent.append("\n");

在循环结束之前添加fileContent.append(“\n”);

#3


0  

use the new line char, \n.

使用新的行char, \n。

where you have this fileContent.append(cellValue);

这里有这个filecontentte .append(cellValue);

should be fileContent.append(cellValue+"\n");

应该fileContent.append(cellValue +“\ n”);

#4


0  

For windows applications like notepad, you need to use a carriage return followed by a new line.

对于像记事本这样的windows应用程序,您需要使用一个回车,后面跟着一行新的行。

fileContent.append("\r\n");