标题: IO流中复制图片的代码整理 [打印本页] 作者: 淡淡柠檬茶 时间: 2014-6-6 12:49 标题: IO流中复制图片的代码整理 //复制一个图片
import java.io.*;
class CopyPic
{
public static void main(String[] args) throws IOException
{
/*方法1
FileOutputStream ou = new FileOutputStream("C:\\.2.jpg");
FileInputStream in = new FileInputStream("C:\\1.jpg");
int num = 0;
while ((num = in.read())!=-1)
{
ou.write(num);
}
ou.close();
in.close();
*/
//方法2
FileOutputStream ou = new FileOutputStream("C:\\.2.jpg");
FileInputStream in = new FileInputStream("C:\\1.jpg");
byte[] bvf = new byte[in.available()];
in.read(bvf);
ou.write(bvf);
ou.close();
in.close();
/*方法3
FileOutputStream ou = null;
FileInputStream in = null;
try
{
ou = new FileOutputStream("c:\\.2.jpg");
in = new FileInputStream("c:\\1.jpg");
//byte[] bvf = new byte[1024];
//int len = 0;
//while((len = in.read(bvf))!=-1)
//{
// ou.write(bvf,0,len);
//}
byte[] bvf = new byte[in.available()];
in.read(bvf);
ou.write(bvf);