Read all files in a folder

Source: Internet
Author: User
Mport Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. file; public class readfile {public readfile () {}/ *** read all files in a folder */public static Boolean readfile (string filepath) throws filenotfoundexception, ioexception {try {file = new file (filepath); If (! File. isdirectory () {system. out. println ("file"); system. out. println ("Path =" + file. getpath (); system. out. println ("absolutepath =" + file. getabsolutepath (); system. out. println ("name =" + file. getname ();} else if (file. isdirectory () {system. out. println ("folder"); string [] filelist = file. list (); For (INT I = 0; I <filelist. length; I ++) {file readfile = new file (filepath + "\" + filelist [I]); If (! Readfile. isdirectory () {system. out. println ("Path =" + readfile. getpath (); system. out. println ("absolutepath =" + readfile. getabsolutepath (); system. out. println ("name =" + readfile. getname ();} else if (readfile. isdirectory () {readfile (filepath + "\" + filelist [I]) ;}}} catch (filenotfoundexception e) {system. out. println ("readfile () exception:" + E. getmessage ();} return true;}/*** delete All folders and files in the folder * // * Public static Boolean deletefile (string delpath) throws filenotfoundexception, ioexception {try {file = new file (delpath); If (! File. isdirectory () {system. out. println ("1"); file. delete ();} else if (file. isdirectory () {system. out. println ("2"); string [] filelist = file. list (); For (INT I = 0; I <filelist. length; I ++) {file delfile = new file (delpath + "\" + filelist [I]); If (! Delfile. isdirectory () {system. out. println ("Path =" + delfile. getpath (); system. out. println ("absolutepath =" + delfile. getabsolutepath (); system. out. println ("name =" + delfile. getname (); delfile. delete (); system. out. println ("successfully deleted");} else if (delfile. isdirectory () {deletefile (delpath + "\" + filelist [I]) ;}} file. delete () ;}} catch (filenotfoundexception e) {system. out. println ("deletefile () exception:" + E. getmessage ();} return true;} */public static void main (string [] ARGs) {try {readfile ("E:/videos "); // deletefile ("D:/file");} catch (filenotfoundexception ex) {} catch (ioexception ex) {} system. out. println ("OK ");}}

 

Read all files in a folder

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.