package com.yc;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
/*
*
* 实现对某一个图片的剪切和粘贴
*
*/
public static byte[] cut(File file)//剪切
{
BufferedInputStream bfd = null;
byte[]b =null;
try
{
bfd = new BufferedInputStream(new FileInputStream(file));
b = new byte[bfd.available()];
bfd.read(b);
}catch(FileNotFoundException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
finally
{
try {
bfd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return b;
}
public static void paste(byte[]b,File file)//粘贴
{
BufferedOutputStream bfw = null;
try
{
bfw = new BufferedOutputStream(new FileOutputStream(file));
bfw.write(b);
}catch(IOException e)
{
e.printStackTrace();
}finally
{
try {
bfw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void apply(File file,File file1)
{
byte b[] = cut(file1);
paste(b,file);
if(file1.exists())
file1.delete();
else
System.out.println("该图片已删除");
}
public static void main(String args[])
{
File file = new File("D:\\2\\2.jpg");//图片要被粘贴的位置,图片名字必须要写,不然会报错
File file1 =new File("D:\\photo\\2.jpg");//被剪切的图片
apply(file,file1);
}
}