Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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 以下上下文中的PrintWriter与FileWriter_Java_Filewriter_Printwriter - Fatal编程技术网

Java 以下上下文中的PrintWriter与FileWriter

Java 以下上下文中的PrintWriter与FileWriter,java,filewriter,printwriter,Java,Filewriter,Printwriter,如果在下一段代码中,我将用(新的PrintWriter)替换(新的PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter ("xanaduindeed.txt"))); pw = new PrintWriter(new BufferedWriter(new PrintWriter ("xanaduindeed.txt"))); 这两种方法都很好,但是我想知道这两种方法中的哪一种优化了内存使用。(如果这两种方法中的

如果在下一段代码中,我将用
(新的PrintWriter
)替换
(新的PrintWriter

pw = new PrintWriter(new BufferedWriter(new FileWriter  ("xanaduindeed.txt")));

pw = new PrintWriter(new BufferedWriter(new PrintWriter ("xanaduindeed.txt")));
这两种方法都很好,但是我想知道这两种方法中的哪一种优化了内存使用。(如果这两种方法中的任何一种都更好)
提前感谢。

我最担心的是,这两种情况下的异常处理是不同的。看看这个老答案


不过,无论哪种情况,都要小心编码!您使用的是系统默认值,因此您在计算机上写的某些内容可能会在另一种情况下被误读。我担心的主要问题是,这两种情况下的异常处理都不一样。请查看此旧答案

不管是哪种情况,都要小心编码!您使用的是系统默认值,因此您在计算机上编写的内容可能会在Oracle JVM的另一个版本中被误读:

public PrintWriter(String fileName) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
         false);
}
PrintWriter
的显著特点是在每一行换行符(LF或CR或CRLF)上刷新输出。最低内存占用率应该是一个裸
FileWriter
,但缓冲可以显著提高I/O性能。

在Oracle的JVM中:

public PrintWriter(String fileName) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
         false);
}
PrintWriter
的显著特点是在每一行换行符(LF或CR或CRLF)上刷新输出。最低内存占用率将是一个裸
FileWriter
,但缓冲可以显著提高I/O性能。

最好的解决方案是

new PrintWriter ("xanaduindeed.txt")));
它在内部使用BufferedWriter,并将文本写入字符输出流,缓冲字符,以便有效地写入单个字符、数组和字符串。您无需进一步尝试和优化它。

最好的解决方案是

new PrintWriter ("xanaduindeed.txt")));

它在内部使用BufferedWriter并将文本写入字符输出流,对字符进行缓冲,以便有效地写入单个字符、数组和字符串。您无需进一步尝试和优化它。

不是
pw=new PrintWriter(new BufferedWriter(new PrintWriter(“xanaduindied.txt”))
pw=new PrintWriter(“xanaduindied.txt”);
相同
pw=new PrintWriter(new BufferedWriter(“xanaduindied.txt”);
pw=new PrintWriter(“xanaduindied.txt”)相同;