java中文件的复制

第一种:使用Java7的Files类复制

                        File dd=new File("D:/Tomcat7.0/webapps/ROOT1523957275296.xml");
File dd2=new File("D:/Tomcat7.0/webapps/333.xml");

Files.copy(dd.toPath(),dd2.toPath());

第二种:使用Commons IO复制

                        File dd=new File("D:/Tomcat7.0/webapps/ROOT1523957275296.xml");
File dd2=new File("D:/Tomcat7.0/webapps/333.xml");
FileUtils.copyFile(dd, dd2);

第三种:使用FileChannel复制

                        File dd=new File("D:/Tomcat7.0/webapps/config.xml");
File dd2=new File("D:/Tomcat7.0/webapps/444.xml");
FileChannel inputStream=null;
     FileChannel outStream=null;
     try {
inputStream = new FileInputStream(dd).getChannel();
outStream=new FileOutputStream(dd2).getChannel();
outStream.transferFrom(inputStream, 0, inputStream.size());
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=outStream){
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

第四种:使用BufferedReader复制

                        File dd=new File("D:/Tomcat7.0/webapps/config.xml");
File dd2=new File("D:/Tomcat7.0/webapps/44.xml");
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader(dd));
bw=new BufferedWriter(new FileWriter(dd2));
String line=null;
while((line=br.readLine())!=null){
bw.write(line+"\n");
}
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=bw){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=br){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

第五种:使用FileStreams复制

                        File dd=new File("D:/Tomcat7.0/webapps/config.xml");
File dd2=new File("D:/Tomcat7.0/webapps/555.xml");
InputStream iputStream=null;
OutputStream outStream=null;
try {
iputStream=new FileInputStream(dd);
outStream = new FileOutputStream(dd2);
byte[] byteData=new byte[1024];
int byteRead=0;
while((byteRead=iputStream.read(byteData))>0){
outStream.write(byteData, 0, byteRead);
}
outStream.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=outStream){
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=iputStream){
try {
iputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值