字节拷贝

来源:互联网 发布:2013年总决赛韦德数据 编辑:程序博客网 时间:2024/06/05 17:24
public static void method1(String path1 , String path2){//创建字节输入流对象FileInputStream fis = null;//创建字节输出流FileOutputStream fos = null;try {fis = new FileInputStream(new File("path1"));fos = new FileOutputStream(new File("path2"));//如果path2不存在自动创建//边读边写 一次拷贝一个字节int hasRead = -1;while((hasRead = fis.read())!=-1){fos.write(hasRead);}try {fis = new FileInputStream(new File("path1"));fos = new FileOutputStream(new File("path2"));//如果path2不存在自动创建//边读边写 一次拷贝一个字节数组byte[] b = new byte[1024];//或者1024的倍数int length = -1;while((length = fis.read(b))!=-1){fos.write(b, 0, length);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//关闭资源 try {if (fos !=null) {fos.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if (fis!=null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}
原创粉丝点击