package com.beiwo.inputstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class lianxi { /**
* @param args
* 练习题: 将一张图片拷贝到另外一个地方。
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
String str1 = "C:\\Users\\cdlx2016\\Desktop\\1\\12字方针.png";
String str2 = "C:\\Users\\cdlx2016\\Desktop\\2\\12字方针.png";
// copyFile1(str1, str2);
// copyFile2(str1, str2);
copyFile3(str1, str2); }
// 方法一
public static void copyFile1(String srcPath, String destPath) throws IOException {
// 打开输入流
FileInputStream fis = new FileInputStream(srcPath);
// 打开输出流
FileOutputStream fos = new FileOutputStream(destPath); // 读取和写入信息
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
} // 关闭流 先开后关 后开先关
fos.close(); // 后开先关
fis.close(); // 先开后关 }
// 方法二
public static void copyFile2(String srcPath, String destPath) throws IOException { // 打开输入流
FileInputStream fis = new FileInputStream(srcPath);
// 打开输出流
FileOutputStream fos = new FileOutputStream(destPath); // 读取和写入信息
int len = 0;
// 创建一个字节数组,当做缓冲区
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b);
} // 关闭流 先开后关 后开先关
fos.close(); // 后开先关
fis.close(); // 先开后关 }
// 方法三
public static void copyFile3(String srcPath, String destPath) throws IOException { // 打开输入流
FileInputStream fis = new FileInputStream(srcPath);
// 打开输出流
FileOutputStream fos = new FileOutputStream(destPath); // 读取和写入信息
int len = 0;
// 创建一个字节数组,当做缓冲区
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
} // 关闭流 先开后关 后开先关
fos.close(); // 后开先关
fis.close(); // 先开后关 } }

最新文章

  1. js基础练习二之简易日历
  2. codeforces 613D:Kingdom and its Cities
  3. JSP知识点汇总
  4. shell自动计算脚本
  5. hdu----(3065)病毒侵袭持续中(AC自动机)
  6. spring+jpg环境下,spring实现文件下载web实现通用的文件下载方法
  7. PHP输出中文乱码的问题
  8. 谈谈HTTP/2对前端的影响【转载】
  9. Mybatis_2.基于XML的增删改查
  10. [十二省联考2019]异或粽子 01trie
  11. srping的历史与哲学
  12. WPF 如何控制右键菜单ContextMenu的弹出
  13. 七、uboot 代码流程分析---C环境建立
  14. 47.HTML---frame,iframe,frameset之间的关系与区别
  15. java.lang.RuntimeException: Fail to connect to camera service
  16. Windows中snmputil.exe工具的使用
  17. 【转】Hudson插件Email-Ext邮件模板时间格式化的解决方法
  18. 【算法】N Queens Problem
  19. ios 使用ASIHTTPRequest来检查版本更新
  20. mysql 使用如下三种方式应用where条件,从好到坏

热门文章

  1. [译]merge vs rebase
  2. CSS背景background详解,background-position详解
  3. 常用的Mysql数据库操作语句大全
  4. 限制帐号同时两处以上登录-ASP.NET
  5. mysql取前取后
  6. git 教程(14)--解决冲突
  7. destoon去掉会员注册email验证
  8. [k]优雅的css
  9. MySQL 5.7 学习:安全相关特性
  10. java 深入技术六(Map)