本文介绍了Java复制部分InputStream到OutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件3236000字节,我想从开始读取2936000,并写入OutputStream

  InputStream is = new FileInputStream(file1); 
OutputStream os = new FileOutputStream(file2);

AFunctionToCopy(is,os,0,2936000); / *一个函数或源代码写入输入流0to2936000字节* /

我可以读写byte字节,但它是缓慢的(我认为)从缓冲的读取
我如何复制呢?

解决方案
  public static void copyStream(InputStream input,OutputStream output,long start,long end)
throws IOException
{
for(int i = 0; i byte [] buffer = new byte [1024]; // Adjust if you want
int bytesRead;
while((bytesRead = input.read(buffer))!= -1&& amp; amp;& amp; amp; amp; R< = end)//测试EOF或结束
{
output.write buffer,0,bytesRead);
}
}

$ b

I have a file with 3236000 bytes and I want to read 2936000 from start and write to an OutputStream

InputStream is = new FileInputStream(file1);
OutputStream os = new FileOutputStream(file2);

AFunctionToCopy(is,os,0,2936000); /* a function or sourcecode to write input stream 0to2936000 bytes */

I can read and write byte by byte, but it's to slow (i think) from buffered readingHow can do I copy it?

解决方案
public static void copyStream(InputStream input, OutputStream output, long start, long end)
    throws IOException
{
    for(int i = 0; i<start;i++) input.read(); // dispose of the unwanted bytes
    byte[] buffer = new byte[1024]; // Adjust if you want
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1 && bytesRead<=end) // test for EOF or end reached
    {
        output.write(buffer, 0, bytesRead);
    }
}

should work for you.

这篇关于Java复制部分InputStream到OutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:31