Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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 删除最后一个字符/行_Java_Swing - Fatal编程技术网

Java 删除最后一个字符/行

Java 删除最后一个字符/行,java,swing,Java,Swing,我有一段代码将文本从文件打印到一个名为textArea的JTextArea 不幸的是,我使用的方法是逐行进行的(不理想),因此我必须在每行后面加上一个\n 现在还可以,但在末尾会创建一个新行 我的守则如下: class menuOpen implements ActionListener { public void actionPerformed(ActionEvent e) { try { File filePath = new

我有一段代码将文本从文件打印到一个名为textArea的JTextArea

不幸的是,我使用的方法是逐行进行的(不理想),因此我必须在每行后面加上一个\n

现在还可以,但在末尾会创建一个新行

我的守则如下:

class menuOpen implements ActionListener {
    public void actionPerformed(ActionEvent e)
        {
        try {
            File filePath = new File("c:\\test.txt");
            FileInputStream file = new FileInputStream(filePath);
            BufferedReader br = new BufferedReader(new InputStreamReader(file));
            String displayText;

            while ((displayText = br.readLine()) != null) {
                textArea.append(displayText + "\n");
            }

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
有人能帮我把最后一行删掉吗?

怎么样

if (textArea.length > 0) textArea.Text = textArea.Text.Substring(0 ,textArea.Text.Length - 1)
另一个想法是:

boolean firstLine = true;
while ((displayText = br.readLine()) != null) {
    if (firstLine) {
        firstLine = false;
    } else {
        textArea.append("\n");
    }
    textArea.append(displayText);
}

想法是在要显示的新行之前添加一个换行符,除了文件的第一行。

显然,您希望在两行之间,而不是每行之后添加一个新行。这意味着您至少应该有两行:

if (d = br.readLine()) != null ) {
    textArea.append(displayText);
    while (d = br.readLine()) != null ) {
        textArea.append( "\n" + displayText);
    }
}
当然,它看起来更复杂。这是因为“中间”比“之后”更复杂。

在循环中:

while ((displayText = br.readLine()) != null) {
    if (textArea.length() > 0)
        textArea.append("\n");
    textArea.append(displayText);
}

i、 e.如果文本区域中已有一些文本,请插入换行。

最简单的方法是不要使用
BufferedReader.readLine()
。例如:

BufferedReader in = new BufferedReader(new FileReader(filePath));
char[] buf = new char[4096];
for (int count = in.read(buf); count != -1; count = in.read(buf)) {
    textArea.append(new String(buf, 0, count));
}
编辑

我以前应该看到过这一点,但更好的方法是让JTextArea读取文件:

BufferedReader in = new BufferedReader(new FileReader(filePath));
textArea.read(in, null);
这仍将在末尾的换行符中读取,但它将规范化文本中的所有行尾(有关如何处理行尾的说明,请参阅)。因此,您可以通过以下方式摆脱尾随换行符:

// line endings are normalized, will always be "\n" regardless of platform
if (textArea.getText().endsWith("\n")) {
    Document doc = ta.getDocument();
    doc.remove(doc.getLength() - 1, 1);
}
那么:

text.substring(0,text.lastIndexOf('\n'));

这很容易。。你只需要稍微调整一下你的代码

String displayText = br.readLine(); 
textArea.append(displayText);
while ((displayText = br.readLine()) != null) {
    textArea.append("\n" + displayText);
}
我相信这段代码能以最低的成本实现您想要的功能

String displayText = br.readLine(); 
textArea.append(displayText);
while ((displayText = br.readLine()) != null) {
    textArea.append("\n" + displayText);
}