Conversion between Java file, object, and byte []

Source: Internet
Author: User
1. import Java. io. bufferedoutputstream; 2. import Java. io. bytearrayinputstream; 3. import Java. io. bytearrayoutputstream; 4. import Java. io. file; 5. import Java. io. fileinputstream; 6. import Java. io. fileoutputstream; 7. import Java. io. ioexception; 8. import Java. io. objectinputstream; 9. import Java. io. objectoutputstream; 10. import Java. io. serializable; 11. 12. public class byte_file_object {13. 14. /** 15. * convert the file to a byte array 16. * @ edittime 11:45:28. */18. public static byte [] getbytesfromfile (file F) {19. if (F = NULL) {20. return NULL; 21 .} 22. try {23. fileinputstream stream = new fileinputstream (f); 24. bytearrayoutputstream out = new bytearrayoutputstream (1000); 25. byte [] B = new byte [1000]; 26. int N; 27. while (n = stream. read (B ))! =-1) {28. out. write (B, 0, n); 29 .} 30. stream. close (); 31. out. close (); 32. return out. tobytearray (); 33 .} catch (ioexception e) {34 .} 35. return NULL; 36 .} 37. 38. /** 39. * Save the byte array as a file 40. * @ edittime 11:45:56. */42. public static file getfilefrombytes (byte [] B, string outputfile) {43. bufferedoutputstream stream = NULL; 44. file file = NULL; 45. try {46. file = new file (Outputfile); 47. fileoutputstream fstream = new fileoutputstream (File); 48. stream = new bufferedoutputstream (fstream); 49. stream. write (B); 50 .} catch (exception e) {51. e. printstacktrace (); 52 .} finally {53. if (stream! = NULL) {54. try {55. stream. close (); 56 .} catch (ioexception E1) {57. e1.printstacktrace (); 58 .} 59 .} 60 .} 61. return file; 62 .} 63. 64. /** 65. * Get object 66 from byte array. * @ edittime 11:46:34 a.m. 67. */68. public static object getobjectfrombytes (byte [] objbytes) throws exception {69. if (objbytes = NULL | objbytes. length = 0) {70. return NULL; 71 .} 72. bytearrayinputstream Bi = new bytearrayinputstream (objbytes); 73. objectinputstream OI = new objectinputstream (BI); 74. return Oi. readobject (); 75 .} 76. 77. /** 78. * Get a byte array from the object 79. * @ edittime 11:46:56 80. */81. public static byte [] getbytesfromobject (serializable OBJ) throws exception {82. if (OBJ = NULL) {83. return NULL; 84 .} 85. bytearrayoutputstream BO = new bytearrayoutputstream (); 86. objectoutputstream oo = new objectoutputstream (BO); 87. oo. writeobject (OBJ); 88. return Bo. tobytearray (); 89 .} 90 .}

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.