Java基础之IO流,通过字节流对媒体文件进行复制操作
import java.io.*;
class ImageCopyDemo
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream(new File("001.png"));
fos = new FileOutputStream(new File("001-copy.png"));
/*
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fos.write(buffer);
*/
//最折中的方法
byte[] buffer = new byte[1024];
int length = 0;
while((length=fis.read(buffer))!=-1)
{
fos.write(buffer);
}
/*
int ch = 0;
while((ch=fis.read())!=-1)
{
fos.write(ch);
}
*/
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(null!=fis)
fis.close();
if(null!=fos)
fos.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
class ImageCopyDemo
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream(new File("001.png"));
fos = new FileOutputStream(new File("001-copy.png"));
/*
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fos.write(buffer);
*/
//最折中的方法
byte[] buffer = new byte[1024];
int length = 0;
while((length=fis.read(buffer))!=-1)
{
fos.write(buffer);
}
/*
int ch = 0;
while((ch=fis.read())!=-1)
{
fos.write(ch);
}
*/
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(null!=fis)
fis.close();
if(null!=fos)
fos.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}