您的位置:首页 > 编程语言 > Java开发

java复制文件

2015-07-10 10:05 441 查看
// 测试复制文件
@Test
public void copyFile() throws Exception {
String srcPath="D:/qqqq.jpg";
String destPath="D:/copyqqqq.jpg";
InputStream input=null;
File srcFile=new File(srcPath);

if(srcFile.exists()){
input = new FileInputStream(srcFile);
writeToLocal(input, destPath);
}
else{
System.out.println("文件不存在........");
return;
}
}

public static void writeToLocal(InputStream in, String destPath) {
FileOutputStream out = null;
int b = 0;
try {
out = new FileOutputStream(destPath);
while ((b = in.read()) != -1) {
out.write(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: