File class--renameto () moves all specified types of files within a folder to another folder

Source: Internet
Author: User

* Due to the Zhang Xiaoxiang -7k monthly salary of one of the cracks _ Traffic light Management System Video Tutorials folder video files are stored in each sub-folder

* Requirements: All video files are moved to Zhang Xiaoxiang -7k monthly salary of one of the test cracked _ Traffic light Management System Video Tutorial folder deleted subfolders of the original video

Public class movefiles {public static void main (String[] args)  {//  todo auto-generated method stubfile f = new file ("D:\\ Baiduyundownload\\ Zhang Xiaoxiang -7k One of the monthly salary test, traffic light Management system Video Tutorial ");//file f = new file (" d:\\baiduyundownload\ \ Preach Wisdom Podcast _ Zhang Xiaoxiang _java Multi-threading and concurrent Library advanced application video tutorial Download ");if  (F.exists ()) MoveFiles2 (F, f.getpath ()); System.out.println ("Done");} Public static void movefiles (File f, string path)  {File[] files =  f.listfiles (); string addname =  "";//  first determine if the name of the   folder contains "_ Traffic", and then in   get the corner mark   Otherwise indexof will return-1 appears substring (-1) with Exception if  (F.getname (). Contains ("_ Traffic"))  {int index =  F.getname (). IndexOf ("_ Traffic");//int index = f.getname (). IndexOf ("Xiang _") +1;addname =  F.getname (). substring (index);} for  (file file : files)  {if  (File.isdirectory ()) moveFiles (File, path); else  {if&nBSP; (File.getname (). EndsWith (". avi"))  {file dest = new file (Path, file.getname () . replace (".",addname +  ".")); File.renameto (dest); System.out.println (Dest.getname ());}  elsefile.delete ();//  Delete redundant files}file.delete ();//  Delete empty folders}}//  If you need to keep   folders that do not contain "_ Traffic"   The code is as follows Public static void movefiles2 (File f, string path)  {File[] files  = f.listfiles (); Int index = f.getname (). IndexOf ("_ Traffic"); String addname = f.getname (). substring (index);for  (file file : files)   {System.out.println (File.getname ());if  (File.isdirectory ()) if  (File.getname (). Contains ("_ Traffic"))//   In this case, it is guaranteed that//  each F object except the outermost file contains "_ Traffic" MoveFiles (File, path);else {if  (File.getname (). EndsWith (". avi"))  {file dest = new file (Path, file.getname (). Replace (".", AddName  +  ".")); File.renameto (dest);// system.out.printlN ("Generate:" +dest.getname ());}  else {system.out.println (File.delete ()  +  ". The deleted file is. " + file.getname ());//  Delete redundant files}}//  Delete empty folders    returns True if and only if files or directories are deleted successfully Otherwise, return FalseSystem.out.println (File.delete ()  +  ", delete the folder is:"  + file.getname ());}}


This article comes from "either desperately, or roll back!" "Blog, be sure to keep this provenance http://jiangzuun2014.blog.51cto.com/8732469/1532184

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.