基本实现
该实现方法利用缓冲容器(字节数组flush)实现分段输入输出
其他方法:不采用缓冲容器而是一个字节一个字节地输入输出
import java.io.*;
public class IPS {
String Iurl, Ourl;
File Isrc, Osrc;
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//辅助变量,显示缓冲容器当前的长度
//int temp;
InputStream Is = null;
OutputStream Os = null;
//构造器,获取输入源与输出目标
public IPS(String Iurl, String Ourl){
this.Iurl = Iurl;
this.Ourl = Ourl;
}
//具体执行
public void Operate(){
try {
//确定源
Isrc = new File(Iurl);//输入源
Osrc = new File(Ourl);//输出目标
//确定流
Is = new FileInputStream(Isrc);
Os = new FileOutputStream(Osrc);
//操作
while((len = Is.read(flush)) != -1){
Os.write(flush,0,len);
Os.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
主类
public class Main {
public static void main(String[] args){
IPS a = new IPS("C:\\Users\\DaluxC\\Desktop\\新建文件夹δ\\u.png", "C:\\Users\\DaluxC\\Pictures\\Saved Pictures\\u.png");
a.Operate();
}
}