Analyzes and copies a file, and changes the file suffix and process suffix during the copy process.

Source: Internet
Author: User

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 ();}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.