第1个回答 2012-05-25
一个简单的文件复制的例子,从源文件中读取,写入目的文件
public static long forJava(File src,File dest) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(src); //建立读取流
FileOutputStream out=new FileOutputStream(dest); //建立写入流
byte[] buffer=new byte[length];
while(true){
int ins=in.read(buffer);
if(ins==-1){
in.close();
out.flush();
out.close();
return new Date().getTime()-time;
}else
out.write(buffer,0,ins);
}
}
第2个回答 推荐于2018-03-13
try
{
FileInputStream in = new FileInputStream("E:\\a.zip");
FileOutputStream os = new FileOutputStream("e:\\b.zip");
byte[] bt = new byte[1024];
while(in.read(bt) != -1)
{
os.write(bt);
}
os.flush();
in.close();
os.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
第3个回答 推荐于2016-04-25
int len=-1;
byte[] bt = new byte[2048]; //可以根据实际情况调整,建议使用1024,即每次读1KB
while(len=(in.read(bt)) != -1) {
os.write(bt,0,len); //建议不要直接用os.write(bt)
}
os.flush();
in.close();
os.close();本回答被提问者和网友采纳