用java实现将许多文件夹下的所有.avi文件拷贝到一个文件夹

本文演示了如何使用Java进行文件操作,包括获取文件后缀名、设置目标路径,并通过资源加载配置属性文件,实现文件读取和复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 package findName;
import java.io.*;
import java.util.Properties;
public class FileDemo3{
 public static String houZhuiName=new Source().getHouzhuiName();
 public static String destPathName=new Source().getsdestPathName(); 
 public static void main(String [] args) throws Exception{
  long time1 = System.currentTimeMillis();  
  InputStream ips=FileDemo3.class.getResourceAsStream("config.properties");
  Properties prop=new Properties();
  prop.load(ips);  
  String sourceName=prop.getProperty("soureceName"); 
  System.out.println(sourceName);
  ips.close();
  File f=new File(sourceName);
  showFile(f);
  long time2 = System.currentTimeMillis();
  System.out.println(time2-time1);
 } 
 public static void showFile(File dir) throws Exception{  
  File [] files=dir.listFiles();
  for(File file:files){
   if(file.isDirectory()){
    showFile(file);
   }   
   else if(file.getName().endsWith(houZhuiName)){
    InputStream is=new FileInputStream(file);
    BufferedInputStream buffIn=new BufferedInputStream(is);
    OutputStream os=new FileOutputStream(destPathName+file.getName());
    BufferedOutputStream buffOut =new BufferedOutputStream(os);
    byte [] buf=new byte[1024];
    int len=0;
    while((len=buffIn.read(buf))!=-1){
     buffOut.write(buf, 0, len);
    }
   }    
  }  
 }
 
}
class Source{ 
  InputStream ips=FileDemo3.class.getResourceAsStream("config.properties");
  Properties prop=new Properties();
  public String getHouzhuiName(){
   try {
    prop.load(ips);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return prop.getProperty("houZhuiName");
  }
  public String getsdestPathName() {
   try {
    prop.load(ips);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return prop.getProperty("destPathName");
   
  }  
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值