파일 복사 및 변환

                            txt     ,      ,renameTo()       ,      。

import java.io.*;

public class changecopy {
	public void IOCopy(String path,String path1){  
        File file = new File(path);  
        File file1 = new File(path1);  
        if(!file.exists()){  
         System.out.println(file.getName()+"     ");  
        }else{  
         System.out.println("  ");  
        }   
        byte[] b = new byte[(int) file.length()];  
        if(file.isFile()){  
        try {  
      FileInputStream is= new FileInputStream(file);  
      FileOutputStream ps= new FileOutputStream(file1);  
      is.read(b);  
      ps.write(b);  
     } catch (Exception e) {  
      e.printStackTrace();  
     }  
       }else if(file.isDirectory()){  
        if(!file.exists())  
         file.mkdir();  
        String[] list = file.list();  
        for(int i=0;i<list.length;i++){  
          this.IOCopy(path+"/"+list[i], path1+"/"+list[i]);  
        }  
       }  
   }  
	 public void renameAll(File file,String fext){
		  File [] files = file.listFiles();
		  for(File f: files) {
		   if(f.isDirectory()) {
			   System.out.println("s");
		    renameAll(f,fext);
		    System.out.println("f");
		   }
		   if(f.isFile()) {
			 System.out.println("sa");
		     String fileStr = f.toString();
		     int dotPoz = fileStr.indexOf(".");
		     fileStr = fileStr.substring(0, dotPoz + 1) + fext;
		     boolean b = f.renameTo(new File(fileStr));
		     System.out.println(fileStr+b);
		   }
		  }
		 }
	public static void main(String args[]){
		changecopy a = new changecopy();
//		a.IOCopy("D:\\lucene\\example", "D:/kankan");
		a.renameAll(new File("D:/kankan"),"txt");
	}
}

좋은 웹페이지 즐겨찾기