Java copy files to the corresponding path

Source: Internet
Author: User

The recent discovery of company deployment projects only requires overwriting individual class files or other individual files. It's a hassle to find the path manually and find the file again every time. So write a copy of the code out of the file. The code is as follows

Package Com.controller;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.FileOutputStream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.text.simpledateformat;import java.util.ArrayList Import Java.util.date;import Java.util.list;public class CopyFile {private static file fils;//allow copy of files type public static St Ring[] Filterfile = {". js", ". jsp", ". Class", ". Java", ". xml", ". Properties", ". sql"};p rivate static long total = 0l;//p Rivate static pattern pattern =//pattern.compile ("[A-z][:]/[a-z]*/omc[0-9a-z]{0,}");p rivate static list<string > L = new arraylist<string> (); /0 Replace Java with class//1-bit url//2 identification number 0. Is add classes configuration file//3 corresponding path static string[] string = new string[4];//Create assigned path private final static String clpth = "webroot\\ Web-inf\\classes ";p rivate final static String clPth2 =" webroot\\web-inf\\ ";p ublic static void Main (string[] ArGS) throws Exception {//Read text information list<string> k = readtxtfile ("D:\\test2.txt"); for (String string:k) {//System.out. println (String), if ("". Equals (string) | | string = = null) {throw new IOException ("Please confirm file exists");} String[] p = getFile (string, 0); String ClassPath = ""; for (int i = l.size (); i > 0; i--) {ClassPath + = L.get (i-1) + "\ \";} String CLASSP = "";//The data of the back copy specifies the time data string df = new SimpleDateFormat ("YYYYMMdd"). Format (new Date ()); File des = null;if ("". Equals (ClassPath)) {//String [2] is 0. Is add classes under the Config file if (p[2].equals ("0")) {CLASSP = p[1] + clPth2 + "\\classes";d es = new File ("d:\\java\\" + df + "\ \" + ClP th2+ "\\classes");} else {CLASSP = p[1];d es = new File ("d:\\java\\" + df + "\" + p[3]);}} else {CLASSP = p[1] + clpth + "\ \" + classpath;des = new File ("d:\\java\\" + df + "\ \" + clpth + "\ \" + ClassPath);} /* * SYSTEM.OUT.PRINTLN ("clpth:" + clpth); * SYSTEM.OUT.PRINTLN ("ClassPath:" + ClassPath); * SYSTEM.OUT.PRINTLN ("CLASSP:" + CLASSP); *///the data that needs to be copied file src = new file (clAssp.replace ("\ \", "\\\\"));//Filterfile=new String []{p[0]};new CopyFile (). CopyFolder (src, des, filterfile, p[0]);// Reset L = new arraylist<string> ();}} /** * Read Text data * **/public static list<string> readtxtfile (String filePath) {list<string> L = new Arraylist<s Tring> (); try {String encoding = "UTF-8"; File File = new file (filePath);//System.out.println (FilePath); if (File.isfile () && file.exists ()) {//To determine if files exist I Nputstreamreader read = new InputStreamReader (new FileInputStream (file), encoding);//considering the encoding format BufferedReader BufferedReader = new BufferedReader (read); String linetxt = Null;while ((linetxt = Bufferedreader.readline ()) = null) {//System.out.println (linetxt.replace ("\ \", "\\\\")); L.add (linetxt.replace ("\ \", "\\\\"));} Read.close ();} else {System.out.println ("The specified file cannot be found");}} catch (Exception e) {System.out.println ("Error reading file contents"); E.printstacktrace ();} return l;} Read files private static string[] GetFile (String path, int flag) {fils = new File (path);//SYSTEM.OUT.PRIntln ("Path:" +path), if (Fils.isfile ()) {//System.out.println ("Fils:" +fils.getname ()); string[0] = Fils.getname (). Replace ("Java", "Class");} else {//folder name if (! ") src ". Equals (Fils.getname ())) {L.add (Fils.getname ());} if ("". Equals (Fils.getname ())) {L.add ("classes");}} if (Fils.getparent (). LastIndexOf ("src")! =-1) {path = Fils.getparent (); if (Path.lastindexof ("src") + 3)! = Path.length ( ) {return getFile (path, 0);} else if ((Path.lastindexof ("src")) <= Path.length ()) {return GetFile (path, 1);}} if (flag = = 1) {//d:\springmvcstring[1] = path.substring (0, Path.lastindexof ("src")). Replace ("src", ""); string[2] = "0"; } else {System.out.println (path), if (Path.lastindexof ("src") >-1) {string[1] = path.substring (0, Path.lastindexof (" src ")); else {///d:\\springmvc01\\webroot\\web-inf\\index.jspstring[1] = Path.replace (string[0], "");} STRING[2] = ""; string[3] = path.substring (Path.indexof ("WebRoot"), Path.indexof (string[0]));} Return string;/* * for (int i = 0; i < fils.listfiles (). length; i++) {for (file file: * Fils.listfiles ()) {///If not a directory, directly add if (!file.isdirectory ()) {* SYSTEM.OUT.PRINTLN (File.getabsol Utepath ()); } else {* SYSTEM.OUT.PRINTLN (Fils.getabsolutepath ());//For directory files, recursive call * GetFile (File.getabsolutepath ());}} } */}/** * * @param folder * @param filterfile * @param fileName * @throws Exception */public void CopyFolder (File srcfol Der, File destfolder,string[] filterfile, String fname) throws Exception {file[] files = srcfolder.listfiles ();//SYSTEM.O Ut.println (Destfolder);//First delete creation destfolder.mkdirs (); for (File file:files) {//System.out.println ("File.getname ():" +file.getname ()), if (File.isfile ()) {if (File.getname (). Equals (fname)) {String pathname = Destfolder + file.separator+ File.getname (); for (String suff:filterfile) {if (Pathname.endswith (Suff)) {File Dest = new File (pathname); File Destpar = Dest.getparentfile ();d estpar.mkdirs (); if (!dest.exists ()) {dest.createnewfile ();} d:\springmvc\webroot\web-inf\index.jsp//D:\java\20160603\WebRoot\index.jsp//in order to prevent renaming and not under the same path Copyif (File.getparent (). substring (file.getparent (). Length ()-4,file.getparent (). Length ( )-1). Equals (Dest.getparent (). substring (dest.getparent (). Length ()-4,dest.getparent (). Length ()-1))) {if ( File.length () = = 0) {throw new IOException ("The file is not allowed to be empty" + ", the file to be processed is:" + file.getparent () + "\ \" + file.getname ());} CopyFile (file, dest);}}}} else {copyfolder (file, Destfolder, Filterfile, fname);}}} /*** * Copy file * * @param src * @param dest * @throws ioexception */private void copyFile (file src, file dest) throws E xception {Bufferedinputstream reader = null; Bufferedoutputstream writer = null;try {reader = new Bufferedinputstream (new FileInputStream (SRC)); writer = new Bufferedo Utputstream (New FileOutputStream (dest)); byte[] buff = new byte[reader.available ()];while ((Reader.read (buff))! =-1) { Writer.write (buff);} Total + = 1;} catch (Exception e) {throw e;} finally {Writer.flush (); Writer.close (); Reader.close ();//Record String temp = "\ncopy:\n" + src + "\tsize:" + Src.lengTh () + "\nto:\n" + dest + "\tsize:" + dest.length () + "\ n complate\n totoal:" + total; SYSTEM.OUT.PRINTLN (temp);}}}

Java copy files to the corresponding path

Related Article

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.