Java将InputStream的一部分复制到OutputStream
我有一个3236000字节的文件,我想从开始读取2936000字节,然后写入输出流Java将InputStream的一部分复制到OutputStream,java,copy,buffer,inputstream,outputstream,Java,Copy,Buffer,Inputstream,Outputstream,我有一个3236000字节的文件,我想从开始读取2936000字节,然后写入输出流 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 */ 我可以一个字节一个字
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 */
我可以一个字节一个字节地读写,但这会减慢(我认为)缓冲读取的速度
如何复制它?公共静态void copyStream(InputStream输入、OutputStream输出、长起点、长终点)
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);
}
}
抛出IOException
{
对于(int i=0;iI)要将InputStream的一部分复制到OutputStreambytesReadyes@好奇,它将处理第一个字节,然后使用1024字节的缓冲区写入outputstream。您还可以查看通道
类。