简体   繁体   中英

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. Some of the lines contain \\n , because I wanted specific breaking when displaying. However, when I do display the code, the \\n are shown instead of breaking the line as they usually do.

I tried replacing the \\n 's with linebreaks by placing the code str.replaceAll( "\\\\\\\\n", System.lineSeperator()); in the while loop, before 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. Any help would be much appreciated.

The code for the .txt reader is below.

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. It is

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

An example of a line from the .txt file is

Welcome!\\n\\n If you wish to proceed, please type the password below.\\nEnjoy your stay!

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) .

I switched replaceAll() with replace() and worked with Pshemo and Nick Vanderhoven's points. I added the line of code str = str.replace("\\\\n", System.lineSeparator()); as suggested by Pshemo, and that's done the trick. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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