Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 jTextArea使用BufferedReader仅保存文本文件中的第一行文本?_Java_Bufferedreader_Jtextarea_Dataoutputstream - Fatal编程技术网

Java jTextArea使用BufferedReader仅保存文本文件中的第一行文本?

Java jTextArea使用BufferedReader仅保存文本文件中的第一行文本?,java,bufferedreader,jtextarea,dataoutputstream,Java,Bufferedreader,Jtextarea,Dataoutputstream,我试图将多行输出保存在一个文本文件中,从我的jTextArea(代码中称为“outputarea”)到我想要的路径,一切正常,但保存的文件不包含整个输出,而只包含文本的第一行。我正在使用“\n”在jtextarea中中断行,同时提供多行输出,这是否会造成任何差异或此代码中的任何其他问题,此代码只是saveAs按钮上的代码,输出来自我创建的其他方法。提前谢谢 private void saveAs() { FileDialog fd = new FileDialog(home.this, "S

我试图将多行输出保存在一个文本文件中,从我的jTextArea(代码中称为“outputarea”)到我想要的路径,一切正常,但保存的文件不包含整个输出,而只包含文本的第一行。我正在使用“\n”在jtextarea中中断行,同时提供多行输出,这是否会造成任何差异或此代码中的任何其他问题,此代码只是saveAs按钮上的代码,输出来自我创建的其他方法。提前谢谢

private void saveAs() {

 FileDialog fd = new FileDialog(home.this, "Save", FileDialog.SAVE);
 fd.show();
 if(fd.getFile()!=null)
 {
 fn=fd.getFile();
 dir=fd.getDirectory();
 filename = dir + fn +".txt";
 setTitle(filename);
 try
 {

 DataOutputStream d=new DataOutputStream(new FileOutputStream(filename));
 holdText = outputarea.getText();
 BufferedReader br = new BufferedReader(new StringReader(holdText));
 while((holdText = br.readLine())!=null)
 {
 d.writeBytes(holdText+"\r\n");
 d.close();
 }
 }
     catch (Exception e)
     {
     System.out.println("File not found");
     }
outputarea.requestFocus();
save(filename);
 }

}

你应该把
d.close()在while循环完成后,因为在使用
DataOutputStream
写入文件中的第一行之后,您正在关闭它,并且不允许它完成整个作业

您甚至可以看到控制台中写入了错误:

找不到文件

这不是因为它找不到您的文件,而是因为在第一次迭代之后的迭代中,它试图写入一个封闭的流。所以只写第一行。因此,更改代码如下:

while ((holdText = br.readLine()) != null) {
    d.writeBytes(holdText + "\r\n");
}
d.close();
我还建议使用
PrintWriter
而不是
DataOutputStream
。然后您可以轻松地将
writeBytes
更改为
println
方法。这样,您就不需要手动将
\r\n
附加到您编写的每一行

另一个很好的提示是使用
try with resource
(如果您使用java 7或更高版本),或者至少使用
finally
块关闭您的流:

String holdText = outputarea.getText();
try (PrintWriter w = new PrintWriter(new File(filename));
     BufferedReader br = new BufferedReader(new StringReader(holdText))) {
    while ((holdText = br.readLine()) != null) {
        w.println(holdText);
    }

} catch (Exception e) {
        System.out.println("File not found");
}

祝你好运。

你应该把
d.关闭()在while循环完成后,因为在使用
DataOutputStream
写入文件中的第一行之后,您正在关闭它,并且不允许它完成整个作业

您甚至可以看到控制台中写入了错误:

找不到文件

这不是因为它找不到您的文件,而是因为在第一次迭代之后的迭代中,它试图写入一个封闭的流。所以只写第一行。因此,更改代码如下:

while ((holdText = br.readLine()) != null) {
    d.writeBytes(holdText + "\r\n");
}
d.close();
我还建议使用
PrintWriter
而不是
DataOutputStream
。然后您可以轻松地将
writeBytes
更改为
println
方法。这样,您就不需要手动将
\r\n
附加到您编写的每一行

另一个很好的提示是使用
try with resource
(如果您使用java 7或更高版本),或者至少使用
finally
块关闭您的流:

String holdText = outputarea.getText();
try (PrintWriter w = new PrintWriter(new File(filename));
     BufferedReader br = new BufferedReader(new StringReader(holdText))) {
    while ((holdText = br.readLine()) != null) {
        w.println(holdText);
    }

} catch (Exception e) {
        System.out.println("File not found");
}

祝您好运。

您首先将holdText分配给整个文本
holdText=outputarea.getText()
然后转到第一行
holdText=br.readLine()
。为单行引入一个新变量,那么一切都应该正常在while循环之外。@statefi可能就是您首先将holdText分配给整个文本的答案
holdText=outputarea.getText()
然后转到第一行
holdText=br.readLine()
。为单行引入一个新变量,那么一切都应该正常在while循环之外。@STaefi可能这就是答案。非常感谢STaefi先生。代码现在可以正常工作了,我更喜欢下一次的PrintWriter,这样更方便…再次感谢!很高兴这有帮助;-)非常感谢斯塔菲先生。代码现在可以正常工作了,我更喜欢下一次的PrintWriter,这样更方便…再次感谢!很高兴这有帮助;-)