简体   繁体   English

Java - 列表 <String> 打印\\ n而不是显示换行符

[英]Java - List<String> printing \n instead of showing a line break

I have a .txt file reader, which reads individuals lines of a file and stores them in a List<String> which I then display on a JTextArea. 我有一个.txt文件阅读器,它读取文件的个别行并将它们存储在List<String> ,然后我将其显示在JTextArea上。 Some of the lines contain \\n , because I wanted specific breaking when displaying. 有些行包含\\n ,因为在显示时我想要特定的断开。 However, when I do display the code, the \\n are shown instead of breaking the line as they usually do. 但是,当我显示代码时,会显示\\n而不是像通常那样打破行。

I tried replacing the \\n 's with linebreaks by placing the code str.replaceAll( "\\\\\\\\n", System.lineSeperator()); 我尝试通过放置代码str.replaceAll( "\\\\\\\\n", System.lineSeperator());替换\\n和换行符str.replaceAll( "\\\\\\\\n", System.lineSeperator()); in the while loop, before list.add(str); 在while循环中,在list.add(str);之前list.add(str); but it doesn't seem to have any effect. 但它似乎没有任何影响。

To reiterate, I simply need a way to change the \\n 's to linebreaks. 重申一下,我只需要一种方法将\\n更改为换行符。 Any help would be much appreciated. 任何帮助将非常感激。

The code for the .txt reader is below. .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();
            }
        }

**Edit - I have updated the post to include a bit more code. **编辑 - 我已更新帖子以包含更多代码。

The List I send in as an argument is initialized before the method. 我作为参数发送的List在方法之前被初始化。 It is 它是

protected static List<String> textfiles = new ArrayList<String>();

An example of a line from the .txt file is .txt文件中的一行示例是

Welcome!\\n\\n If you wish to proceed, please type the password below.\\nEnjoy your stay! 欢迎!\\ n \\ n如果您想继续,请在下面输入密码。\\ n享受您的住宿!

The code to display this text is below. 显示此文本的代码如下。 (Pardon formatting) (原谅格式)

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

(Posted on behalf of the OP) . (代表OP发布)

I switched replaceAll() with replace() and worked with Pshemo and Nick Vanderhoven's points. 我用replace()切换了replaceAll()并使用了Pshemo和Nick Vanderhoven的观点。 I added the line of code str = str.replace("\\\\n", System.lineSeparator()); 我添加了代码行str = str.replace("\\\\n", System.lineSeparator()); as suggested by Pshemo, and that's done the trick. 正如Pshemo建议的那样,这就完成了。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM