/* * *能 复制图片,声音文件,视频文件 */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); }}