Analyzes and copies a file, and changes the file suffix and process suffix during the copy process.
This is my own practice:
Copy all. java files in one directory to another, but change the file suffix during the copy process.
Idea: just like the other one, the action is slightly different and you can directly use 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 \\"); // specify the source File filename = new File ("E: \"); // The target renameToFile (dir, filename ); // call method} private static void renameToFile (File dir, File filename) throws IOException {File [] files = dir. listFiles (); // load all files in the source Directory into an array. ArrayList <File> al = new ArrayList <File> (); // defines a set that stores a File object. FileInputStream FCM; // byte read stream FileOutputStream fos; // byte output stream for (File file: files) {// variable if (file. isDirectory () renameToFile (file, filename); // if the file to be traversed is a directory, continue to call this method. Elseif (file. getName (). endsWith (". java") // if it is. java, add the file to the List set. Al. add (file) ;}for (File file: al) {// variable String name = file for the set. getName (). replace (". java ",". txt "); // replace .javain the set with. txt. <Pre name = "code" class = "javascript"> <span style = "white-space: pre"> </span> File newFile = new File (filename, name ); // encapsulate the Directory and the replaced name into a File, which is used for writing the file. The name defined at the time of writing is "FS = new FileInputStream (File)"; // read fos = new FileOutputStream (newFile ); // write int len = 0; byte [] buf = new byte [1024]; while (len = Fi. read (buf ))! =-1) {fos. write (buf, 0, len);} FD. close (); fos. close ();}}}