java 如何通过 BufferedOutputStream 写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25270259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write in file by BufferedOutputStream?
提问by vimalraturi
I want to copy data from demo1.txt
to demo2.txt
, although I can do it by BufferedReader
, I want to copy by BufferedInputStream
/ BufferedOutputStream
. Please show me how to do this.
我想将数据从复制demo1.txt
到demo2.txt
,虽然我可以做到这一点BufferedReader
,我想通过复制BufferedInputStream
/ BufferedOutputStream
。请告诉我如何做到这一点。
import java.io.*;
class stream4
{
public static void main(String arr[])
{
BufferedInputStream bfis=new BufferedInputStream(new FileInputStream("demo1.txt"));
BufferedOutputSteam bfos=new BufferedOutputStream(new FileOutputStream("demo2.txt"));
byte b[]=(bfis.read());
bfos.write(b);
bfis.close();
bfos.close();
}
}
回答by SparkOn
change
改变
byte b[]=(bfis.read());
to
到
byte[] b = new byte[1024];
try {
for (int readNum; (readNum = bfis.read(b)) != -1;) {
bfos.write(b, 0, readNum);
}
} catch (IOException ex) {
ex.printStackTrace();
}
finally {
bfis.close();
bfos.close();
}
as bfis.read()
the next byte of data, or -1 if the end of the stream is reached.
作为bfis.read()
数据的下一个字节,如果到达流的末尾,则为 -1。
回答by Adam
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main
{
public static void main(String[] args) throws Exception
{
String fromFileName = "demo1.txt";
String toFileName = "demo2.txt";
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fromFileName));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(toFileName));
byte[] buff = new byte[32 * 1024];
int len = 0;
while ((len = in.read(buff)) > 0) //If necessary readLine()
out.write(buff, 0, len);
in.close();
out.close();
}
}
This will do the job :). Just specify what kind of byte size you are looking at, and from there use a loop to continue to read the file.
这将完成工作:)。只需指定您正在查看的字节大小,然后使用循环继续读取文件。
回答by VirtualVAT
As others correctly suggested you need your own buffer actually to read and write by portions, which is represented as byte array of the specified size. So this now make no sense in wrapping your FileInputStream and FileOutputStream with BufferedInputStream and BufferedOutputStream - they are useful if you input from stream and output by smaller portions. I suggest just making your buffer bigger than suggested (say, 16384 or 32768) and remove unnecessary BufferedInputStream and BufferedOutputStream in this case.
正如其他人正确建议的那样,您实际上需要自己的缓冲区来按部分读取和写入,这表示为指定大小的字节数组。因此,现在将 FileInputStream 和 FileOutputStream 与 BufferedInputStream 和 BufferedOutputStream 包装在一起是没有意义的 - 如果您从流中输入并按较小的部分输出,它们会很有用。我建议只让你的缓冲区比建议的大(比如 16384 或 32768),并在这种情况下删除不必要的 BufferedInputStream 和 BufferedOutputStream。