This is my own approach:
Copy all the. java files under one directory to another directory, but change the file suffix name during replication.
Idea: Follow the same, just slightly slightly different action, directly on the code
Package Pack;import java.util.*;import java.io.*;class copyrenamefile{public static void Main (string[] args) throws Ioexception{file dir=new file ("E:\\javase\\java review code \ \");//explicit source file Filename=new file ("e:\\");//Purpose Renametofile (dir, filename);//call method}private static void Renametofile (File dir,file filename) throws ioexception{file[] Files= Dir.listfiles ();//load all the files in the source directory into an array. Arraylist<file>al=new arraylist<file> ();//defines a collection where the collection stores a File object. FileInputStream fis;//Byte read stream fileoutputstream fos;//byte output stream for (File file:files) {//array variable if (file.isdirectory ()) Renametofile (file,filename);//If the traversed file is a directory, continue calling the method. ElseIf (File.getname (). EndsWith (". Java"))//if it is. Java, add the file to the list collection. Al.add (file);} The for (File file:al) {//sets variable string name=file.getname (). replace (". Java", ". txt");//Replaces. Java in the collection with. txt. <pre name= "Code" class= "JavaScript" ><span style= "White-space:pre" ></span>file newfile=new File ( Filename,name);//encapsulates the directory with the replaced name as file, which is used to write the name Fis=new fileinputstream (file);//Read fos=new FilEoutputstream (newFile);//write int len=0;byte[] buf=new byte[1024];while ((Len=fis.read (BUF))!=-1) {Fos.write (Buf,0,len );} Fis.close (); Fos.close ();}}}
Parse the copy file and change the file suffix name during the copy process