- public class FileCoy {
- public static void main(String[] args) {
-
- System.out.print("请输入文件路径:");
- Scanner sc = new Scanner(System.in);
-
- String path = sc.next();
- copy(path);
-
- }
-
- public static void copy(String filepath){
-
- File f = new File(filepath);
- File[] ff = f.listFiles();
-
- String path = "D:\\新建文件夹\\test2";
-
- for(int i=0;i<ff.length;i++){
- if(!ff[i].isDirectory()){
- if(ff[i].getName().endsWith(".txt")){
- String name = ff[i].getName().substring(0,ff[i].getName().indexOf("."));
- String filename = name+".java";
- File f2 = new File(path+"\\"+filename);
- BufferedInputStream fis = null ;
- BufferedOutputStream fos = null ;
- try {
- fis = new BufferedInputStream(new FileInputStream(ff[i]));
- fos = new BufferedOutputStream(new FileOutputStream(f2));
- byte[] buf = new byte[2024];
- int j ;
- while((j=fis.read(buf)) != -1){
- fos.write(buf,0 , j);
- }
- fos.flush();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
- }
- }
-
- }
复制代码
|
|