java中复制一个文件,并修改后缀等

public class FileIOTest{

    public void copyFile ( String filePath ) {
        int len = 0 ;
        FileInputStream fis = null ;
        FileOutputStream fos = null ;
        File file = new File(filePath) ;
        try{
            fis = new FileInputStream( filePath ) ;
            String newFileName = file.getPath().replaceAll("\\.txt", ".xxx");
            fos = new FileOutputStream( new File( newFileName ) );

            byte[] bt = new byte[1024];
            while ( ( len = fis.read( bt )) != -1){
                fos.write( bt , 0 , len );
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                fis.close();
                fos.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main (String args[]) throws Exception {
        FileIOTest ft=  new FileIOTest() ;
        ft.copyFile("D:\\111.txt");
    }
}

你可能感兴趣的:(Java复制文件)