Java: A simple file toolkit and a java toolkit

Source: Internet
Author: User

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 ();}}}}

  

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.