Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-List<;字符串>;打印\n而不是显示换行符_Java_String_Swing_Text - Fatal编程技术网

Java-List<;字符串>;打印\n而不是显示换行符

Java-List<;字符串>;打印\n而不是显示换行符,java,string,swing,text,Java,String,Swing,Text,我有一个.txt文件读取器,它读取文件中的每一行,并将它们存储在列表中,然后显示在JTextArea上。有些行包含\n,因为我希望在显示时具体中断。但是,当我显示代码时,会显示\n,而不是像通常那样断行 我试着用换行符替换\n,方法是放置codestr.replaceAll(“\\\\n”,System.lineseparator())在while循环中,在列表之前添加(str)但它似乎没有任何效果 重申一下,我只需要一种方法将\n更改为换行符。任何帮助都将不胜感激 下面是.txt读取器的代码

我有一个.txt文件读取器,它读取文件中的每一行,并将它们存储在
列表中,然后显示在JTextArea上。有些行包含
\n
,因为我希望在显示时具体中断。但是,当我显示代码时,会显示
\n
,而不是像通常那样断行

我试着用换行符替换
\n
,方法是放置code
str.replaceAll(“\\\\n”,System.lineseparator())在while循环中,在
列表之前添加(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();
            }
        }
(代表OP发布)

我将
replaceAll()
切换为
replace()
,并与Pshemo和Nick Vanderhoven的points合作。我添加了代码行
str=str.replace(“\\n”,System.lineSeparator())完成了。干杯

(代表OP发布)


我将
replaceAll()
切换为
replace()
,并与Pshemo和Nick Vanderhoven的points合作。我添加了代码行
str=str.replace(“\\n”,System.lineSeparator())完成了。干杯

我想多看看你的代码会有帮助。请考虑张贴A。我认为\n是字面上写在他的档案里。我们需要输入文件和替换代码,我想看看发生了什么。
str.replaceAll(\\\\n,System.lineseparator())
(或者更好的是
str.replace(\\n,System.lineseparator())
,以避免显式正则表达式转义)应该用替换的
\n
创建新字符串。您确定正在使用此方法的结果吗?也许您忘记将其存储回
str
引用(如
str=str.replace…
),所以
str
仍然包含其原始字符串?无论如何,这是唯一的出路。如果我知道你的话,我会尽力防止这个问题,而不是创建变通解决方案。@Nick Vanderhoven没错,\n在.txt文件中。@ThomasRichmond如果文件是你创建的-不要自己插入换行符。只需转到一个新的行-其余的将被处理。我认为这将有助于看到更多的代码。请考虑张贴A。我认为\n是字面上写在他的档案里。我们需要输入文件和替换代码,我想看看发生了什么。
str.replaceAll(\\\\n,System.lineseparator())
(或者更好的是
str.replace(\\n,System.lineseparator())
,以避免显式正则表达式转义)应该用替换的
\n
创建新字符串。您确定正在使用此方法的结果吗?也许您忘记将其存储回
str
引用(如
str=str.replace…
),所以
str
仍然包含其原始字符串?无论如何,这是唯一的出路。如果我知道你的话,我会尽力防止这个问题,而不是创建变通解决方案。@Nick Vanderhoven没错,\n在.txt文件中。@ThomasRichmond如果文件是你创建的-不要自己插入换行符。只要换一条新的线路,其余的都会处理好的。
protected static List<String> textfiles = new ArrayList<String>();
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();
      }