Parse the copy file and change the file suffix name during the copy process

Source: Internet
Author: User

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

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.