Java: A simple file toolkit and a java toolkit
Class FileUtils {// total number of files in the File directory public static int fileNumber (File dir) {int filenumber = 0; if (dir. exists () {for (File file: dir. listFiles () {if (file. isDirectory () {filenumber = filenumber + fileNumber (file);} else {filenumber ++ ;}} return filenumber ;} // determine whether the File is an image public static boolean isImage (File imgFilePath) {try {FileInputStream imgfis = new FileInputStream (imgFilePath); byte [] imgbyte = New byte [imgfis. available ()]; if (imgfis. read (imgbyte ))! =-1) {if (imgbyte [0] = (byte) 'G' & imgbyte [1] = (byte) 'I & imgbyte [2] = (byte) 'F') {return true;} else if (imgbyte [1] = (byte) 'P' & imgbyte [2] = (byte) 'n' & imgbyte [3] = (byte) 'G') {return true ;} else if (imgbyte [6] = (byte) 'J' & imgbyte [7] = (byte) 'F' & imgbyte [8] = (byte) 'I' & imgbyte [9] = (byte) 'F') {return true;} else {return false ;}} catch (Exception e) {Syst Em. out. println (e. toString (); return false;} // returns the array of public static File [] listAllDirectory (File dir) {if (dir! = Null & dir. exists () {File [] finalfile = new File [fileNumber (dir)]; int markfile = 0; int fileln = 0; File files [] = dir. listFiles (); for (int I = 0; I <files. length; I ++) {if (files [I]. isDirectory () {listAllDirectory (files [I]);} else {finalfile [markfile ++] = files [I] ;}} return finalfile ;} else {return null ;}// copy the File (using the File channel) public static void copyFile (File oldFileAbsolutePath, File newFilePath) {File newFileAbsolutePath = new File (newFilePath, oldFileAbsolutePath. getName (); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try {fi = new FileInputStream (oldFileAbsolutePath ); fo = new FileOutputStream (newFileAbsolutePath); in = fi. getChannel (); out = fo. getChannel (); in. transferTo (0, in. size (), out);} catch (IOException e) {e. printStackTrace ();} finally {try {fi. close (); in. close (); fo. close (); out. close ();} catch (IOException e) {e. printStackTrace ();}}}}