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();
}
}
}
}

你可能感兴趣的:(copy)