copy 文件

 

留着,免得以后忘记

/**
* copy 文件
* @param srcPath
* @param tagPath
*/
public void copyFile(String srcPath , String tagPath){
System.out.println("copy file start ...");
BufferedInputStream in = null ;
BufferedOutputStream out = null ;
// String fileName = tagPath.substring(tagPath.lastIndexOf("/")+1);
String filePath = tagPath.substring(0 , tagPath.lastIndexOf("/"));
File tagFilePath = new File(filePath);
if(!tagFilePath.exists()){
tagFilePath.mkdirs();
}
try {
int bytesum = 0;
int byteread = 0;
in = new BufferedInputStream(new FileInputStream(srcPath));
out = new BufferedOutputStream(new FileOutputStream(tagPath));
byte[] buffer = new byte[2048];
while((byteread = in.read(buffer))!=-1){
out.write(buffer, 0, byteread);

}
System.out.println("copy file success ...");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("copy file fail ... , " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println("copy file fail ... , " + e.getMessage());
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

posted @ 2014-06-14 15:11  學亦漫長  阅读(299)  评论(0编辑  收藏  举报