Java File,object,byte[]间转换
2011-12-06 23:35
239 查看
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. * 文件转化为字节数组 16. * @EditTime 2007-8-13 上午11:45:28 17. */ 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. * 把字节数组保存为一个文件 40. * @EditTime 2007-8-13 上午11:45:56 41. */ 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. * 从字节数组获取对象 66. * @EditTime 2007-8-13 上午11:46:34 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. * 从对象获取一个字节数组 79. * @EditTime 2007-8-13 上午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. }
相关文章推荐
- Java中File,byte[],Object间的转换
- Java中File,byte[],Object间的转换
- Java_File,byte[],Object间的转换
- Java中File,byte[],Object间的转换
- java中File,byte[],Object间的转换
- java File和Byte[]数组 相互转换
- 【java】java 中 byte[]、File、InputStream 互相转换
- JAVA 把File文件转换成Byte[]数组的标准写法
- 【转载】java输入流转为文件 InputStreamToFile 以及 byte[]和InputStream的相互转换
- Java File和byte数据之间的转换
- JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
- JAVA基础学习(二十二)--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
- java 中 byte[]、File、InputStream 互相转换
- 转转转--Java File和byte数据之间的转换
- Object,byte[],ByteBuffer之间的转换。
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- 使用json-lib jsonObject 把json 转换成 java对象
- String byte[] stream File之间的相互转换
- 往服务器连续写7个字节的数据作为命令,但是javabyte类型的范围为-128-127。这样要转换
- java中byte数组与int类型的转换(两种方式)