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