Java复制文件工具类
2018-07-20 来源:open-open
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtils {
public FileUtils() {
// TODO Auto-generated constructor stub
}
/**
* @warning The name of file must be end with .xls
* @param res The resource file
* @param des The destination
* @return
* @throws FileNotFoundException
*/
public static boolean toCopy(String res,String des){
boolean flag=false;
Boolean bool1 = res.endsWith(".xxx");
Boolean bool2 = des.endsWith(".xxxb");
if(!bool1 && !bool2){
return false;
}
//输入源文件
File file = new File(res) ;
FileInputStream fr=null;
//复制目标文件
File desFile = new File(des);
FileOutputStream bw=null;
try {
fr = new FileInputStream(file);
bw = new FileOutputStream(desFile);
//buffer
byte[] b = new byte[512];
while(fr.read(b)!=-1){
bw.write(b);
}
bw.flush();
flag=true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fr != null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
}
把.xxx格式的文件复制到指定路径。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:YII2中的get和post
下一篇:比较全的数据库操作PHP类
最新资讯
热门推荐