Conversion of byte arrays and files to each other

Source: Internet
Author: User

1     /** 2 * Gets the byte array of the specified file3      */  4 5     Private byte[] getBytes (String filePath) {6         byte[] buffer =NULL; 7         Try {  8File File =NewFile (FilePath); 9FileInputStream FIS =Newfileinputstream (file); TenBytearrayoutputstream BOS =NewBytearrayoutputstream (1000);  One             byte[] B =New byte[1000];  A             intN;  -              while((n = fis.read (b))! =-1) {   -Bos.write (b, 0, N);  the             }   - Fis.close ();  - Bos.close ();  -Buffer =Bos.tobytearray ();  +}Catch(FileNotFoundException e) { - E.printstacktrace ();  +}Catch(IOException e) { A E.printstacktrace ();  at         }   -         returnbuffer;  -}

1     /** 2 * Based on byte array, generate file3      */  4      Public Static voidGetFile (byte[] bfile, String filepath,string fileName) {  5Bufferedoutputstream BOS =NULL; 6FileOutputStream fos =NULL; 7File File =NULL; 8         Try {  9File dir =NewFile (FilePath); Ten             if(!dir.exists () &&dir.isdirectory ()) {//determine if the file directory exists One Dir.mkdirs ();  A             }   -File =NewFile (filepath+ "\ \" +fileName);  -FOS =Newfileoutputstream (file);  theBOS =NewBufferedoutputstream (FOS);  - Bos.write (bfile);  -}Catch(Exception e) { - E.printstacktrace ();  +}finally {   -             if(Bos! =NULL) {   +                 Try {   A Bos.close ();  at}Catch(IOException E1) { - E1.printstacktrace ();  -                 }   -             }   -             if(Fos! =NULL) {   -                 Try {   in Fos.close ();  -}Catch(IOException E1) { to E1.printstacktrace ();  +                 }   -             }   the         }   *}

Conversion of byte arrays and files to each other

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.