java io 複製圖片 音頻,視頻 都好用啊

/*
 * 
 *能 複製圖片,聲音文件,視頻文件
 */package exe.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Copy1 {
 public void  copy(File srcFile, File targetFile) 
 {
 /**
 * srcFile爲源文件,targetFile目標文件
 * 
 * */

 if(srcFile == null || targetFile == null)
 {
 System.out.println("文件打開失敗");
 return;
 }
 // 2.創建管道
 OutputStream os = null;
 FileInputStream is = null;
 try {
 os = new FileOutputStream(targetFile);
 is = new FileInputStream(srcFile);
 // 3.讀取操做
 // 先創建緩衝區,爲1024個字節大小
 byte [] b = new byte[1024];
 int len = 0;
 // 採用循環的方式讀取文件的內容
 while ((len= is.read(b)) != -1)
 {
 String data = new String (b, 0, b.length);
 // System.out.println(data);
 os.write(b);
 }
 } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }finally{
 // 通常先開的後關
 try {
 if(os != null)
 os.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }finally{
 try {
 if(is != null)
  is.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

 }
 }
 public static void main(String [] args) throws IOException
 {
 File srcFile = new File("C:\\Users\\Administrator\\Desktop\\1.jpg
 ");
 File targetFile = new File("C:\\Users\\Administrator\\2.jpg");
 new Copy1().copy(srcFile, targetFile);
 }
}
相關文章
相關標籤/搜索