import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class lianxi {
/**
* 练习题: 将一张图片拷贝到另外一个地方。
*/
public static void main(String[] args) throws IOException {
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(); // 先开后关
}
}
java 将一张图片拷贝到另外一个地方。(IO流)
猜你喜欢
转载自blog.csdn.net/a3060858469/article/details/80545693
今日推荐
周排行