folder to search for files and copy or move them to the specified directory

Source: Internet
Author: User

To write a gadget, the requirement is to search for a file of the specified keyword from a folder, copy or move the search results to a new directory, maintain the original directory structure, and assign the keyword to be responsible or mobile

import;import;import;import;import;import;import;import java.nio.channels.filechannel;import java.util.arraylist;import  java.util.calendar;import java.util.list;import java.util.scanner;public class  textcopyutil {static int countfiles = 0;//  A variable that declares the number of statistical files static int  countfolders = 0;//  declare the variables of the statistics folder static string srcfolderpath =  "d:\\\\ fast disk";//   Default directory static string destfolderpath =  "d:\\\\1360downloads";//  target directory static  string keyword =  "Meeting Minutes";static string type =  "copy";p ublic static  void main (String[] args)  {// java The main entrance of the program System.out.println ("Start now to manipulate batch files"); SYSTEM.OUT.PRINTLN ("You have to process the file as:" +args[0]); List<string> result = txt2stringlist (New file (args[0)); System.out.print ("\ t default directory"); System.out.print ("\ t target directory"); System.out.print ("\ t keyword"); System.out.println ("\ t processing mode (move is mobile, copy represents replication)");for  (String string : result)  {string[ ] arr = string.split ("\ t"); string srcfolderpath = arr[0];//  default directory string destfolderpath = arr[1];//   Target directory string keyword = arr[2]; string type = arr[3]; System.out.print ("\ t" +srcfolderpath); System.out.print ("\ t" +destfolderpath); System.out.print ("\ t" +keyword); System.out.println ("\ t" +type); Scanner input = new scanner (; System.out.println ("Please enter the number 1 if consistent with the expected, otherwise other numbers"); String name = input.nextline (), if ("1". Equals (name)) {System.out.println (Calendar.getinstance (). Gettimeinmillis ()); run (Srcfolderpath, destfolderpath, keyword, type); System.out.println (Calendar.getinstance (). Gettimeinmillis ());}}} public static&nbsp List<string> txt2stringlist (File file)  {list<string> result = new  ArrayList<String> (); Try {bufferedreader br = new bufferedreader (new  FileReader (file);//  constructs a BufferedReader class to read the file string s = null;while  ((s =  Br.readline ())  != null)  {//  use ReadLine method, read one line at a time Result.add (s);} Br.close ();}  catch  (exception e)  {e.printstacktrace ();} Return result;} /** *  perform search and copy  *  @param  srcFolderPath  search directory  *  @param  destfolderpath   Copy destination directory  *  @param  keyword  keyword  */public static void run (String  srcfolderpath, string destfolderpath,string keyword,string type)  {File  Srcfolder = new file (srcfolderpath);//  default directory File destfolder = new file ( Destfolderpath);//  target directory if  (!srcfolder.exists ())   {//  If the folder does not exist System.out.println ("directory does not exist:"  + srcfolder.getabsolutepath ()); return;} if  (!destfolder.exists ())  {//  If the folder does not exist mkdir (Destfolder); System.out.println ("Directory Creation succeeded:"  + destfolder.getabsolutepath ());} File[] result = searchfile (Srcfolder, keyword);//  call method to get the file array System.out.println ("in   " + srcFolder + "   and all sub-files when looking for object " + keyword"; System.out.println ("looked up"  + countFiles +  "  files,"  + countFolders+  "   folders, find   " + result.length + "   Eligible files: "); SYSTEM.OUT.PRINTLN ("Start copying all objects found above");for  (int i = 0; i < result.length;  i++)  {//  Cycle display file file file = result[i]; File destfile = new file (File.getabsolutepath (). Replacefirst (srcFolderPath,  Destfolderpath), if (type!=null&& "move". Equals (Type)) {//move file.renameto (DestFile);} else{//Copy FileChannelCopy (file, destfile);} System.out.println (File.getabsolutepath ()  +    Copy to   + destfile.getabsolutepath ());//   Show file absolute path}}/** *  Create folder  *  *  @param  file */public static  void mkdir (File file)  {if  (File.getparentfile (). exists ())  {if  (! File.exists ())  {file.mkdir ();}}  else {mkdir (File.getparentfile ()); File.mkdir ();}} /** *  copying files using a file channel  *  *  @param  s *              source files  *  @param  t *              copied to the new file  */public static void filechannelcopy ( file s, file t)  {mkdir (T.getparentfile ()); fileinputstream fi = null; fileoutputstream fo = null; filechannel in = null; filechannel out = null;try {fi =  New fileinputstream (s); Fo = new fileoutputstream (t); In = fi.getchannel ();//   Get corresponding file Channel Out = fo.getchannel ();//  get corresponding file channel In.transferto (0, in.size (),  out);//   Connect two channels and read from the in channel and write to the Out Channel} catch  (ioexception e)  {e.printstacktrace ();}  finally {try {fi.close (); In.close (); Fo.close (); Out.close ();}  catch  (ioexception e)  {e.printstacktrace ();}} /** *  recursively find files that contain keywords  *  *  @param  folder *  @param  keyWord  *  @return  */public static file[] searchfile (file folder, final string  keyword)  {file[] subfolders = folder.listfiles (New filefilter ()  {//  Use the internal anonymous class to get file Public boolean accept (file pathname)  {//  implement the FileFilter class's Accept Method if  ( Pathname.isfile ())//  if it is a file countfiles++;else//  if it is a directory countfolders++;if  (pathname.isdirectory () ||  (Pathname.isfile ()  && pathname.getname (). toLowerCase (). Contains (Keyword.tolowercase () )))//  directory or file containing the keyword Return true;return false;}); List result = new arraylist ();//  declares a collection if  (subfolders != null)  { for  (file subfolder : subfolders)  {//  Cycle display folder or file if  (Subfolder.isfile ())  {//  if it is a file, add the file to the results list Result.add (subfolder);}  else {//  if it is a folder, call this method recursively, and then add all the files to the results list file[] foldresult = searchfile ( Subfolder, keyword);if  (foldresult != null)  {for  (file fold :  Foldresult)  {//  Cycle display file Result.add (fold);//  file saved to collection}}}}}file files[] = new  File[result.size ()];//  declares the file array, length is the length of the collection Result.toarray (files);//  The collection is an array of return files;}}

The effect is as follows:

The contents of the read rule file are attached

D:\ Fast plate \[mobilebackup]d:\\1360downloads.mp3copyd:\ Fast plate \[mobilebackup]\[pictures]\picturesd:\\1360downloads.pngcopy

folder to search for files and copy or move them to the specified directory

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.