在Java中,如何使用缓冲流附加到文件?
我有以下代码,但我不确定我在效率/刷新/关闭流方面做的每件事都是正确的。一些建议会很有帮助,谢谢在Java中,如何使用缓冲流附加到文件?,java,java-io,bufferedinputstream,bufferedoutputstream,Java,Java Io,Bufferedinputstream,Bufferedoutputstream,我有以下代码,但我不确定我在效率/刷新/关闭流方面做的每件事都是正确的。一些建议会很有帮助,谢谢 OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file, true)); byte[] buf = new byte[32 * 1024]; // should this be 32KB? while ((in.rea
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file, true));
byte[] buf = new byte[32 * 1024]; // should this be 32KB?
while ((in.read(buf)) > 0) {
out.write(buf);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
最重要的问题是,您忽略了读取的字节数
for(int len; (len = in.read(buf)) > 0;)
out.write(buf, 0, len);
如果您不使用假定的长度,您将始终准确读取32KB,这是一个很大的假设
当您有大量的小写入时,缓冲区非常有用 BufferedOutputStream的默认缓冲大小为8KB,如果您的写入比这个小得多,即小于512字节,那么它们确实会有所帮助 然而,如果你在写32KB的东西,他们可能什么也没做,或者没有帮助。我会把它们拿出来 顺便说一句,如果没有缓冲区,就不需要调用flush() BTW2
最重要的问题是,您忽略了读取的字节数
for(int len; (len = in.read(buf)) > 0;)
out.write(buf, 0, len);
如果您不使用假定的长度,您将始终准确读取32KB,这是一个很大的假设
当您有大量的小写入时,缓冲区非常有用 BufferedOutputStream的默认缓冲大小为8KB,如果您的写入比这个小得多,即小于512字节,那么它们确实会有所帮助 然而,如果你在写32KB的东西,他们可能什么也没做,或者没有帮助。我会把它们拿出来 顺便说一句,如果没有缓冲区,就不需要调用flush() BTW2
最重要的问题是,您忽略了读取的字节数
for(int len; (len = in.read(buf)) > 0;)
out.write(buf, 0, len);
如果您不使用假定的长度,您将始终准确读取32KB,这是一个很大的假设
当您有大量的小写入时,缓冲区非常有用 BufferedOutputStream的默认缓冲大小为8KB,如果您的写入比这个小得多,即小于512字节,那么它们确实会有所帮助 然而,如果你在写32KB的东西,他们可能什么也没做,或者没有帮助。我会把它们拿出来 顺便说一句,如果没有缓冲区,就不需要调用flush() BTW2
最重要的问题是,您忽略了读取的字节数
for(int len; (len = in.read(buf)) > 0;)
out.write(buf, 0, len);
如果您不使用假定的长度,您将始终准确读取32KB,这是一个很大的假设
当您有大量的小写入时,缓冲区非常有用 BufferedOutputStream的默认缓冲大小为8KB,如果您的写入比这个小得多,即小于512字节,那么它们确实会有所帮助 然而,如果你在写32KB的东西,他们可能什么也没做,或者没有帮助。我会把它们拿出来 顺便说一句,如果没有缓冲区,就不需要调用flush() BTW2 从“它是否工作”的角度来看,您的代码似乎还可以。。。然而,您可以通过使用try-with-resource使is看起来“更漂亮”。 您提供的代码将基本转变为以下内容:
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true)) {
byte[] buf = new byte[1024];
while ((in.read(buf)) > 0) {
out.write(buf);
}
out.flush();
}
这是一个Java7特性,如果流资源实现java.lang.AutoCloseable,那么它将自动关闭
根据您试图做的事情,以下几点可能是更简单的解决方案
PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
从“它是否工作”的角度来看,您的代码似乎还可以。。。然而,您可以通过使用try-with-resource使is看起来“更漂亮”。
您提供的代码将基本转变为以下内容:
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true)) {
byte[] buf = new byte[1024];
while ((in.read(buf)) > 0) {
out.write(buf);
}
out.flush();
}
这是一个Java7特性,如果流资源实现java.lang.AutoCloseable,那么它将自动关闭
根据您试图做的事情,以下几点可能是更简单的解决方案
PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
从“它是否工作”的角度来看,您的代码似乎还可以。。。然而,您可以通过使用try-with-resource使is看起来“更漂亮”。
您提供的代码将基本转变为以下内容:
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true)) {
byte[] buf = new byte[1024];
while ((in.read(buf)) > 0) {
out.write(buf);
}
out.flush();
}
这是一个Java7特性,如果流资源实现java.lang.AutoCloseable,那么它将自动关闭
根据您试图做的事情,以下几点可能是更简单的解决方案
PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
从“它是否工作”的角度来看,您的代码似乎还可以。。。然而,您可以通过使用try-with-resource使is看起来“更漂亮”。
您提供的代码将基本转变为以下内容:
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true)) {
byte[] buf = new byte[1024];
while ((in.read(buf)) > 0) {
out.write(buf);
}
out.flush();
}
这是一个Java7特性,如果流资源实现java.lang.AutoCloseable,那么它将自动关闭
根据您试图做的事情,以下几点可能是更简单的解决方案
PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
谢谢我使用Java8获得以下代码的“未处理异常”:try(OutputStream out2=new BufferedOutputStream(new FileOutputStream(file,true)){…}谢谢。我使用Java8获得以下代码的“未处理异常”:try(OutputStream out2=new BufferedOutputStream(new FileOutputStream(file,true)){…}谢谢。我使用Java8获得以下代码的“未处理异常”:try(OutputStream out2=new BufferedOutputStream(new FileOutputStream(file,true)){…}谢谢。我使用Java8获得了带有以下代码的“未处理异常”:try(OutputStream out2=new BufferedOutputStream(new FileOutputStream(file,true)){…}