- package com.ms.test;
- import java.io.*;
- public class ChangeFileName {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File file=new File("e:\\mt4");
- File fileobject=new File("e:\\mt2");
- changeFileName(file,fileobject);
- }
- private static void changeFileName(File file,File fileobject) {
- // TODO Auto-generated method stub
- File[]files=file.listFiles();
- for(int x=0;x<files.length;x++){
- if(files[x].isFile()&&files[x].getName().endsWith("java")){
- String ss=files[x].getName().replaceAll("(.+[\\.])[\\w]+", "$1txt");//正则处理文件名
- File fileob=new File(fileobject.getAbsolutePath()+"\\"+ss);
- try {
- copy(files[x],fileob);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- private static void copy(File file, File fileob) throws Exception {
- // TODO Auto-generated method stub
- FileInputStream fis=new FileInputStream(file);
- FileOutputStream fos=new FileOutputStream(fileob);
- int len=0;
- byte[] by=new byte[1024];
- while((len=fis.read(by))!=-1){
- fos.write(by, 0, len);
- fos.flush();
- }
- fis.close();
- fos.close();
-
- }
- }
复制代码 |