[英]Java - List<String> printing \n instead of showing a line break
我有一个.txt文件阅读器,它读取文件的个别行并将它们存储在List<String>
,然后我将其显示在JTextArea上。 有些行包含\\n
,因为在显示时我想要特定的断开。 但是,当我显示代码时,会显示\\n
而不是像通常那样打破行。
我尝试通过放置代码str.replaceAll( "\\\\\\\\n", System.lineSeperator());
替换\\n
和换行符str.replaceAll( "\\\\\\\\n", System.lineSeperator());
在while循环中,在list.add(str);
之前list.add(str);
但它似乎没有任何影响。
重申一下,我只需要一种方法将\\n
更改为换行符。 任何帮助将非常感激。
.txt阅读器的代码如下。
static void parseStringArray(final String filePath, List<String> list){
try {
InputStream input = Text.class.getResourceAsStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String str;
while((str = reader.readLine()) != null){
list.add(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
**编辑 - 我已更新帖子以包含更多代码。
我作为参数发送的List在方法之前被初始化。 它是
protected static List<String> textfiles = new ArrayList<String>();
.txt文件中的一行示例是
欢迎!\\ n \\ n如果您想继续,请在下面输入密码。\\ n享受您的住宿!
显示此文本的代码如下。 (原谅格式)
Timer teletypeTimer = null;
public static void animateTeletype(final JTextArea displayArea)
{
final String[] s = new String[1];
s[0] = "";
final int[] i = new int[2];
i[0] = 0;
i[1] = 0;
teletypeTimer = new Timer(20, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(i[0]==0)
displayArea.setText("");
s[0] = TTqueue[i[1]].substring(i[0], i[0]+1);
i[0]++;
displayArea.append(s[0]);
if(displayArea.getText().equals(TTqueue[i[1]]))
{
i[1]++;
if(TTqueue[i[1]] !=null)
{
teletypeTimer.stop();
i[0] = 0;
timerRestart(5000, teletypeTimer);
}
else
{
Arrays.fill(TTqueue, null);
complete=true;
teletypeTimer.stop();
}
}
}
});
teletypeTimer.start();
}
(代表OP发布) 。
我用replace()
切换了replaceAll()
并使用了Pshemo和Nick Vanderhoven的观点。 我添加了代码行str = str.replace("\\\\n", System.lineSeparator());
正如Pshemo建议的那样,这就完成了。 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.