package study;
//拷贝文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Exercise {
public static void main(String[] args) {
abc("E:\\临时\\BBB.txt","E:\\临时\\2019.6.4\\bbb.txt");
}
public static void abc(String p1,String p2) {//p1的内容复制到p2,亦可复制图片等文件
//1.创建源
File f1=new File(p1);
File f2=new File(p2);
//2.选择流
InputStream i=null;
OutputStream o=null;
try {
i=new FileInputStream(f1);
o=new FileOutputStream(f2);
//3.操作
byte[] b=new byte[3];
int len=-1;
while((len=i.read(b))!=-1) {
o.write(b, 0, len);
}
o.flush();
}catch(Exception e) {
e.printStackTrace();
}finally {
//先打开后关闭
//记得加null判断,否则会报错
try {
if(null!=o) {
o.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null!=i) {
i.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
20 拷贝文件(可以是图片,音乐,视频等等)
最新推荐文章于 2024-08-11 10:01:49 发布
该博客主要围绕文件拷贝展开,涉及图片、音乐、视频等多种类型文件的拷贝操作,属于信息技术中文件处理相关内容。
5409

被折叠的 条评论
为什么被折叠?



