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